0
votes

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

  • I've made a customer 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 result as TRUE.

SO i decided to do it with 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 common issue that they say that its successfully done But when i go to DB table its empty.

Surprising thing is when i copy the generated query from $VARIABLE using XDEBUGGER and run it directly in sql server . It successfuly gets inserted.

One more surprising thing if i fail any integrity constracint in my query. It says that query have 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 its doing nothing .

Then i noticed in the database that if i put a record manually its has different auto increment id . So basically what i found out every time i try to insert record. It gets inserts in 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 its getting vanished after inserting somehow .

So can anyone help me out solving this issue ?