0

I have a crazy issue that my record in the database is not getting saved.

  • I've made a custom module. Which has model - And i am using it in a observer event before saving the shipment from admin panel

OrderParams.php

<?php

namespace Vendor\Module\Model;

use Magento\Cron\Exception;
use Magento\Framework\Model\AbstractModel;


class RequestParams extends AbstractModel
{
    /**
     * @var \Magento\Framework\Stdlib\DateTime
     */
    protected $_dateTime;

    /**
     * @return void
     */
    protected function _construct()
    {
        $this->_init(\Vendor\Module\Model\ResourceModel\OrderParams::class);
    }
}

ResourceMode/OrderParams.php

<?php

namespace Vendor\Module\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;


class OrderParams extends AbstractDb
{
    /**
     * Initialize resource
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('orders_params', 'id');
    }


}
  • So if I use model for saving a record

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $reqParams=$objectManager->create('Vendor\Module\Model\OrderParams');

          try {
              /*$reqParams->setData([
                  'order_id' => 1,
                  'company_id' => 1
              ]);*/
              $reqParams->setOrderId(1);
              $reqParams->setCompanyId(1);
              $reqParams->save();
          }
          catch (\Magento\Framework\Exception\LocalizedException $e)
          {
             $aa=$e->getMessage();
          }
    

So if you see above I have tried both ways. And I have tried this with or without object manager as well. Still no luck. It doesn't even say that anything went wrong. It returns the result as TRUE.

SO I decided to do it with a direct SQL query

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();

        $insertData[] = ['order_id' => 14, 'company_id' => 1];
        $insertData[] = ['order_id' => 14, 'company_id' => 2];

        try {
            $connection->beginTransaction();
            $connection->insertMultiple('orders_params', $insertData);
            $connection->commit();
        } catch(\Exception $e) {
            $connection->rollBack();
        }

And i did it with insertMultiple() and direct SQL string query and insertArray().. All went failed and all have a common issue that they say that it's successfully done But when I go to the DB table it's empty.

The surprising thing is when I copy the generated query from $VARIABLE using XDEBUGGER and run it directly in the SQL server. It successfully gets inserted.

One more surprising thing is if I fail any integrity constraint in my query. It says that the query has an issue and MySQL will not insert it.

Then I tried this in my other class which is basically a CARRIER model. And there it worked without having any change in the code. Just by copying and pasting it there.

But in my model, it's doing nothing.

Then I noticed in the database that if I put a record manually its has a different auto-increment id. So basically what I found out every time I try to insert a record. It gets inserts in the table. But somehow it gets deleted automatically.

For the confirmation, I turned on QUERY LOGGER in PHPMyAdmin and I found out my query there for inserting. But I did not find any delete query. But still, it's getting vanished after inserting somehow.

So can anyone help me out solving this issue?

1
  • Thanks for the reply. I had already tried cache flushing. Even I ran all the setup upgrade, di complile and static content deploy in case. But still no luck Commented Aug 30, 2021 at 8:02

1 Answer 1

1

I think Magento cache issue that's reason value not updated in Database table so please try to flush and clean Magento cache and then check.

1
  • Thanks for the reply. I had already tried cache flushing. Even I ran all the setup upgrade, di complile and static content deploy in case. But still no luck Commented Sep 5, 2021 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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