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