3

I have created a custom module and trying to save data but not saved data into custom_table. I don't know why? I compared same code with my previous Modules Everything is same.

Here is my installSchema.php

 public function install(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        if (!$installer->tableExists('table_name')) {
            $table = $installer->getConnection()->newTable(
                $installer->getTable('table_name')
            )->addColumn(
                'row_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                [
                    'primary'  => true,
                    'nullable' => false,
                    'unsigned' => false,
                ],
                'Missing Order ID'
            )->addColumn(
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                [
                    'nullable' => false,
                    'unsigned' => false,
                ],
                'Qoute ID'
            )->addColumn(
                'reserved_order_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                [
                    'nullable' => false,
                    'unsigned' => false,
                ],
                'Reserved Order ID'
            )->addColumn(
                'customer_email',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                100,
                [
                    'nullable' => false,
                ],
                'Costumer Email'
            )->addColumn(
                'created_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_DATE,
                null,
                [

                    'nullable' => false,
                ],
                'Order Date'
            )->addColumn(
                'base_grand_total',
                \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                null,
                [
                    'nullable' => false,
                ],
                'Grand Total'
            )->setComment('Missing Orders');
            $installer->getConnection()->createTable($table);
        }


 $installer->endSetup();
}

here is my code to save data

public function execute(Observer $observer)
    {
        $orderIds = $observer->getEvent()->getOrderIds();
        $order    = $this->orderFactory->create()->load($orderIds[0]);
        $quote    = $this->_quoteFactory->create()->load($order->getQuoteId());
        $order    = null;
        if (empty($order)) {
            try {
                $rowData = $this->_missingOrdersFactory->create();
                $rowData->setEntityId($quote->getEntityId());
                $rowData->setReservedOrderId($quote->getReservedOrderId());
                $rowData->setCustomerEmail($quote->getCustomerEmail());
                $rowData->setCreatedAt($quote->getCreatedAt());
                $rowData->setBaseGrandTotal($quote->getBaseGrandTotal());
                $rowData->save();
                echo "<pre>";
                print_r($rowData->getData());
                exit;
            } catch (\Exception $e) {
                echo $e->getMessage();
            }

        }
    }

print_r($rowData->getDdata) is returning me

Array
(
    [entity_id] => 200
    [reserved_order_id] => 000000168
    [customer_email] => waqarcui@gmail.com
    [created_at] => 2019-07-03 04:29:03
    [base_grand_total] => 105.0000
)

Thanks.

  • Can you please make sure that you are getting empty $order? – poojan sharma Jul 3 at 4:43
  • yes my code interrupts at exit() it means if condtion is true – Waqar Ali Jul 3 at 4:44
  • Did you do cache flush? – Nasir Perwaiz Jul 3 at 4:55
  • yes i really did this\ – Waqar Ali Jul 3 at 4:58
5

You're not setting the 'identity' => true in your installschema.php, that's why it won't auto-increment since you're not adding data to row_id that may be the reason your data is not saving.

Your primary key field should be like this.

          ->addColumn(
                    'row_id',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'nullable' => false,
                        'primary'  => true,
                        'unsigned' => true,
                    ],
                    'Missing Order ID'
                )

Hope it helps.

  • if i add data to database using sql its showing fine in admin Grid – Waqar Ali Jul 3 at 4:49
  • ok can you share your whole file code for saving data rather than piece of code and can you please tell me what are you getting in $order and $quote variable – Mohit Rane Jul 3 at 4:54
  • please check updated answer – Mohit Rane Jul 3 at 5:09
  • 1
    checkout_onepage_controller_success_action – Waqar Ali Jul 3 at 5:22
  • 1
    thank you its working fine now – Waqar Ali Jul 3 at 6:00

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.