3

I would like to get a request parameter in the observer file.

Below is the observer code.

etc/frontend/di.xml

<event name="review_save_before">
    <observer name="set_customer_data" instance="Namespace\Modulename\Observer\ReviewSaveBeforeObserver"/>
</event>

Observer/ReviewSaveBeforeObserver

<?php
namespace Namespace\Modulename\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Review\Model\Review;
use Psr\Log\LoggerInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Framework\App\Request\Http;

class ReviewSaveBeforeObserver implements ObserverInterface
{

    /**
     * @var Http
     */
    private $request;

    /**
     * @var OrderInterface
     */
    private $orderInterface;

    /** @var \Magento\Framework\Logger\Monolog */
    protected $logger;

    public function __construct(
        LoggerInterface $loggerInterface,
        OrderInterface $orderInterface,
        Http $request
    ) {
        $this->logger = $loggerInterface;
        $this->orderInterface = $orderInterface;
        $this->request = $request;
    }

    /**
     * This is the method that fires when the event runs.
     *
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        $this->logger->addDebug('Executed Review Save After Observer');

        /** @var Review $review */
        $review = $observer->getDataByKey('object');
        echo '<pre>';print_r($this->request->getParams());die;
        //$review->setStatusId(Review::STATUS_APPROVED);            
        $review->setCustomerId(1);        
    }
}

But it is throwing below error.

Fatal error: Uncaught Exception: User Error: Some transactions have not been committed or rolled back in /var/www/html/CE/2.3.0/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3955 in /var/www/html/CE/2.3.0/vendor/magento/framework/App/ErrorHandler.php:61 Stack trace: #0 [internal function]: Magento\Framework\App\ErrorHandler->handler(256, 'Some transactio...', '/var/www/html/C...', 3955, Array) #1 /var/www/html/CE/2.3.0/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(3955): trigger_error('Some transactio...', 256) #2 [internal function]: Magento\Framework\DB\Adapter\Pdo\Mysql->__destruct() #3 {main} thrown in /var/www/html/CE/2.3.0/vendor/magento/framework/App/ErrorHandler.php on line 61

Any help would be appreciated!

5
  • 2
    code looks fine. Are you sure error generated becoz of using $this->request only or it can be something else ?
    – Yash Shah
    Apr 26, 2019 at 4:42
  • Yes, it is a generating error due to getParamas() Apr 26, 2019 at 4:44
  • Using this observer I want to set the customer ID from the request params, if I pass static value then it is working fine. Apr 26, 2019 at 4:46
  • 1
    I think using getParams() will not throw error Some transactions have not been committed or rolled back, Try this, use RequestInterface object and get only params from the object without doing any other operation. You can put this code in try.. catch to get exact error details. Apr 26, 2019 at 4:51
  • Thanks to everyone. and especially Jaimin Sutariya and Yash Shah. My issue is resolved. I do not dam sure what is the reason for this issue because I have a 2 issue in my code. 1) I print in the observer and 2) I have used OrderInterface to get value in Observer. after removing OrderInterface & print_r(). my issue is resolved. Apr 26, 2019 at 6:02

4 Answers 4

2

It is because you have printed information in the observer. I think it is not because you have used getRequest() in the observer.

Try to see the result in this way.

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(print_r($this->request->getParams(), true));

And check in the test.log file. Do not print anything in the observer.

2

Try this way:

/**
 * @var \Magento\Framework\App\RequestInterface
 */
public $request;

public function __construct(
    ...
    \Magento\Framework\App\RequestInterface $request
){
    ...
    $this->request = $request;
}

public function execute(Observer $observer)
{
    $post = $this->request->getPost();
                /* or */
    $param = $this->request->getParam('parameter');
}
1

Try using

\Magento\Framework\App\RequestInterface::getParams()  

In replacement for:

\Magento\Framework\App\Request\Http::getParams()
2
  • Thanks, I already try this but it is not working. get params working fine in the controller, but it is not working in Observer. I just want to to know why it is not working in the observer? Apr 26, 2019 at 4:33
  • I'm not really sure as error is actually not related, can you try to comment this $review = $observer->getDataByKey('object'); in your current code and check.
    – fmsthird
    Apr 26, 2019 at 5:12
1

Did you try with :

//\Magento\Framework\App\RequestInterface
$this->_request->getParam('field_name');

Its working for me. Hope it will work for you too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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