Hello I have created admin grid for my custom module, I have a form where it is possible to add new product comments. The thing is that I have Product id field, where u can enter id for which product to post comment. So if non-existent id is entered and saved the page breaks.
This is my save controller:
<?php namespace Vendor\Module\Controller\Adminhtml\Comments; use Magento\Backend\App\Action; use Magebit\ProductComments\Model\Comments; use Magento\Catalog\Api\ProductRepositoryInterface; class Save extends Action { private $productRepository; protected $_model; /** * @param Action\Context $context */ public function __construct( Action\Context $context, Comments $model, ProductRepositoryInterface $productRepository ) { parent::__construct($context); $this->_model = $model; $this->productRepository = $productRepository; } /** * {@inheritdoc} */ /** * Save action * * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { $data = $this->getRequest()->getPostValue(); /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); if ($data) { /** @var \Vendor\Module\Model\Comments $model */ $model = $this->_model; $id = $this->getRequest()->getParam('id'); if ($id) { $model->load($id); } $model->setData($data); $this->_eventManager->dispatch( 'productcomments_comments_prepare_save', ['comments' => $model, 'request' => $this->getRequest()] ); try { $model->save(); $this->messageManager->addSuccess(__('Comment successfully saved')); $this->_getSession()->setFormData(false); if ($this->getRequest()->getParam('back')) { return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId(), '_current' => true]); } return $resultRedirect->setPath('*/*/'); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addError($e->getMessage()); } catch (\RuntimeException $e) { $this->messageManager->addError($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addException($e, __('Something went wrong while saving the comment')); } $this->_getSession()->setFormData($data); return $resultRedirect->setPath('*/*/edit', ['comment_id' => $this->getRequest()->getParam('id')]); } return $resultRedirect->setPath('*/*/'); } } I was thinking about using productrepository getByID function, but haven't yet figured it out. Hope someone will have answers for me, thanks!
Error:
[error] 997#997: *2510 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined variable: mageRunCode in /var/www/magento/public/pub/index.php on line 45 PHP message: PHP Notice: Undefined variable: mageRunType in /var/www/magento/public/pub/index.php on line 46" while reading response header from upstream, client: 127.0.0.1, server: magento.test, request: "GET /admin_1c63d9/mui/index/render/key/d4e9ab2bc3f8dae59f5c2d8d7d9c810149e8b2cbe127f209f27a92a316f958ae/?namespace=notification_area&sorting%5Bfield%5D=created_at&sorting%5Bdirection%5D=asc&isAjax=true HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "magento.test", referrer: "http://magento.test/admin_1c63d9/admin_comments/comments/edit/id/24/key/897a6e8fe837ce25195df20fa34bd77ee1be78c70e2e7e2f592f97e33de5fb7f/"