Hello Friends!!!
In this blog, we will learn how we can save a custom column’s value in a product review details record table in the database. To achieve this functionality, we will follow the below steps:
1. We will create our custom_column in the review_detail record table in the app/code/Vendor/Module/etc/db_schema.xml file.
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<!--Adding custom_column field in review_detail table-->
<table name="review_detail" resource="default" comment="Review Detail">
<column xsi:type="int" name="custom_column"
unsigned="true" padding="2"
nullable="false" default="0"
comment="Custom Column"/>
</table>
</schema>
2. Now, we will create an events.xml file inside the app/code/Vendor/Module/etc directory. And we will add our custom observer class for the review_save_after event.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="review_save_after">
<observer name="wk_update_product_review_after_observer"
instance="Vendor\Module\Observer\SaveReviewAfter"/>
</event>
</config>
3. Now, we will create a SaveReviewAfter.php file inside the app/code/Vendor/Module/Observer directory.
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface;
class SaveReviewAfter implements ObserverInterface
{
/**
* @var ManagerInterface
*/
private $_messageManager;
/**
* @param ManagerInterface $messageManager
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\App\ResourceConnection $resource
*/
public function __construct(
ManagerInterface $messageManager,
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\App\ResourceConnection $resource
) {
$this->_request = $request;
$this->_resource = $resource;
$this->_messageManager = $messageManager;
}
/**
* Execute Method to save user id in review_detail record table
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
try {
$review = $observer->getEvent()->getDataObject();
$connection = $this->_resource;
$value = 12;
$tableName = $connection->getTableName('review_detail');
$detail = [
'custom_column' => $value,
];
$select = $connection->getConnection()->select()
->from($tableName)
->where('review_id = :review_id');
$detailId = $connection->getConnection()
->fetchOne($select, [':review_id' => $review->getId()]);
if ($detailId) {
$condition = ["detail_id = ?" => $detailId];
$connection->getConnection()->update($tableName, $detail, $condition);
} else {
$detail['store_id'] = $review->getStoreId();
$detail['customer_id'] = $review->getCustomerId();
$detail['review_id'] = $review->getId();
$connection->getConnection()->insert($tableName, $detail);
}
} catch(Exception $e){
$this->_messageManager->addError($e->getMessage());
}
}
}
Now, we will execute the setup upgrade command and flush the cache, then we will save the review and check the result.
Hope this will be helpful.
Thanks 🙂

Be the first to comment.