0

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');
}
}
2

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.

9
  • Thanks for your response I have added your code. My primary field name is id but it still added the the new row for same records.@Abdul – Prits Aug 27 '20 at 14:57
  • Any one question is i will not send any $data['id'] so how will it check it database will you please help. – Prits Aug 27 '20 at 14:59
  • I have updated my model file as well @Abdul please check – Prits Aug 27 '20 at 15:03
  • Assuming Order Id as a unique field in your table, I have updated my answer. Main concept is in order to update a record you need to load the record model either by load() or by collection(). – Abdul Pathan Aug 28 '20 at 5:59
  • Thanks it solves my issue @Abdul – Prits Aug 28 '20 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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