Question

I have created a custom table and update data using model and resource model.

But it always create a new row on hitting ajax how to update row as old data is appear. I think i want to use Update query instead of insert but not have idea where should i change.

My controller file is

<?php
namespace Tym17\AdminSample\Controller\Index;
use Magento\Framework\App\Action\Context;
use Tym17\AdminSample\Model\ReviewsFactory;
class Index extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
protected $reviews;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    ReviewsFactory $reviews,
    \Magento\Framework\View\Result\PageFactory $pageFactory)
{
    $this->_pageFactory = $pageFactory;
    $this->reviews = $reviews;
    return parent::__construct($context);
}

public function execute()
{
    
    $data = $this->getRequest()->getParams();
    $reviews = $this->reviews->create();
    $reviews->setData('orderid', $data['order_id']);  
    $reviews->setData('status', $data['payment_status']);  
    if ($reviews->save()) {
        $this->messageManager->addSuccessMessage(__('You saved review'));
    } else {
        $this->messageManager->addErrorMessage(__('Review was not saved.'));
    }
}

}

My model file path ( app/code/Tym17/AdminSample/Model/ResourceModel/Reviews.php )

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Tym17\AdminSample\Model\ResourceModel;

class Reviews extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
public function _construct()
{
    $this->_init('status', 'id');
}
}
Was it helpful?

Solution

In Order to update the record you need to make sure that the Primary field of the table is provided to load the model. If the primary field is set to null Magento will create a new record.

Let us assume primary field of your table is named as id. Now the updated code will be

public function execute()
{
    
    $data = $this->getRequest()->getParams();
    $reviews = $this->reviews->create();
    /* Load record by **id** if it is already available in database*/
    /*if(isset($data['id']) && $data['id'] != null ){
       $reviewsModel = $reviews->load($data['id']);
    }*/
    /* Load record by **Order Id** if it is already available in database*/
    $reviewsModel = $reviews->load($data['order_id'], 'orderid');

    
    $reviewsModel->setData('orderid', $data['order_id']);  
    $reviewsModel->setData('status', $data['payment_status']);  
    if ($reviewsModel->save()) {
        $this->messageManager->addSuccessMessage(__('You saved review'));
    } else {
        $this->messageManager->addErrorMessage(__('Review was not saved.'));
    }
}

FYI : You can see in your Model/ResourceModel/Reviews.php that you have declared an _construct() method something like this.

protected function _construct()
{
    $this->_init('table_name', 'primary_field_name');
}

Hope it was helpful. Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top