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!
getParamas()
getParams()
will not throw errorSome transactions have not been committed or rolled back
, Try this, useRequestInterface
object and get only params from the object without doing any other operation. You can put this code intry.. catch
to get exact error details.Jaimin Sutariya
andYash 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.