2

I have created custom module for product review. I have added additional coloumn in review_detail table in DB. I got all submitted values from form including my custom column value in Controller. But Now How Do I Save the additional column value in DB which means how do I pass the value to DB to that associated created column

 public function execute()
    {
        $message = null;
        $status = null;
        if (!$this->formKeyValidator->validate($this->getRequest())) {
            $message = __("Invalid form key");
            $status = 'failed';
        } else {
            //get the form data
            $data = $this->reviewSession->getFormData(true);
            if ($data) {
                $rating = [];
                if (isset($data['ratings']) && is_array($data['ratings'])) {
                    $rating = $data['ratings'];
                }
            } else {
                $data = $this->getRequest()->getPostValue();

                $detail = $this->getRequest()->getPostValue('detail');
                $data['detail'] = trim($detail);
                $rating = $this->getRequest()->getParam('ratings', []);
            }
            if (($product = $this->initProduct()) && !empty($data)) {
                /** @var \Magento\Review\Model\Review $review */

                $review = $this->reviewFactory->create()->setData($data);
                $review->unsetData('review_id');
                $validate = $review->validate();
                if ($validate === true) {
                    try {
                        $review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
                                ->setEntityPkValue($product->getId())
                                ->setStatusId(Review::STATUS_PENDING)
                                ->setCustomerId($this->customerSession->getCustomerId())
                                ->setStoreId($this->storeManager->getStore()->getId())
                                ->setStores([$this->storeManager->getStore()->getId()])
                                ->save();
                        foreach ($rating as $ratingId => $optionId) {
                            $this->ratingFactory->create()
                                    ->setRatingId($ratingId)
                                    ->setReviewId($review->getId())
                                    ->setCustomerId($this->customerSession->getCustomerId())
                                    ->addOptionVote($optionId, $product->getId());
                        }
                        $review->aggregate();
                        $status = 'success';
                    } catch (\Exception $e) {
                        $this->reviewSession->setFormData($data);
                        $message = __('We can\'t post your review right now.');
                        $status = 'failed';
                    }
                } else {
                    $this->reviewSession->setFormData($data);
                    if (is_array($validate)) {
                        foreach ($validate as $errorMessage) {
                            $message = $errorMessage;
                        }
                    } else {
                        $message = __('We can\'t post your review right now.');
                    }
                    $status = 'failed';
                }
            }
        }
        $result['messages'] = $message;
        $result['status'] = $status;
        $resultJson = $this->_resultJsonFactory->create();
        $resultJson->setData($result);
        return $resultJson;
    }
1

I would say that the best solution is to create a plugin to the review model

You could create a beforeSave method and check if your parameters are being submitted. If so, you just set the parameters you want e.g.: $subject->setData('mydata', 'myvalue')

After that, all your custom date should be saved automatically.

For more information, check Magento devDocs - Plugins (Interceptors)

  • Thank you for your response.I have saved my additional field value in DB – Jaisa Feb 9 '18 at 16:58
  • Now i need to get that field value in list.phtml which shows the existing customer reviews in product page – Jaisa Feb 9 '18 at 16:59
  • I have got the collection. But failed to get my additional field value – Jaisa Feb 9 '18 at 17:00
  • try using the getCustomAttribute method e.g.: $model->getCustomAttribute('your_attribute_name'); if ($attribute) { $value = $attribute->getValue(); } – Thiago Lima Feb 9 '18 at 17:13
  • @Sri Could you set this answer as correct? – Thiago Lima Feb 11 '18 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.