2

I have created multiple table schema using setup script like below.

Setup/InstallSchema.php

   namespace Vendor\Module\Setup;
   use Magento\Framework\Setup\InstallSchemaInterface;
   use Magento\Framework\Setup\ModuleContextInterface;
   use Magento\Framework\Setup\SchemaSetupInterface;

 class InstallSchema implements InstallSchemaInterface
{

  public function install(SchemaSetupInterface $setup, 
   ModuleContextInterface $context)
  {
    /**
     * Create table 'tbl_test1'
     */
    $table = $setup->getConnection()
        ->newTable($setup->getTable('tbl_test1'))
        ->addColumn(
            'id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Pod ID'
        )
        ->addColumn(
            'name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => false, 'default' => ''],
            'name'
        );
    $setup->getConnection()->createTable($table);

   /**
     * Create table 'tbl_test_2'
     */
    $table = $setup->getConnection()
        ->newTable($setup->getTable('tbl_test_2'))
        ->addColumn(
            'id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            ''
        )
        ->addColumn(
            'mage_order_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => false, 'default' => ''],
            'magento orderid'
        );
    $setup->getConnection()->createTable($table);

I need to insert multiple records for each table.

How this can be achieved using installer. Can anyone look into this and update me your answer please.

5
  • You wanna to insert records into your table ?? Commented Apr 2, 2019 at 5:52
  • @PrathapGunasekaran, yes I have created 2 tables, need to insert multiple records for each table Commented Apr 2, 2019 at 5:53
  • For adding data to the tables you required installData or UpgradeData please follow this Commented Apr 2, 2019 at 5:55
  • @ABHISHEKTRIPATHI, can you update me InstallData.php code for my tables just with sample data please Commented Apr 2, 2019 at 5:56
  • I think you should try with default Magento way github.com/magento/magento2/blob/… Commented Apr 2, 2019 at 6:27

1 Answer 1

3

You can use an InstallData script
Try this:

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

       $tableName = $setup->getTable('tbl_test1');
       $tableName2 = $setup->getTable('tbl_test_2');

        $data = [
            [
            'id' => NULL,
            'frameName' => 'black box'
            ],
            [
            'id' => NULL,
            'frameName' => 'white box'
            ]
        ];

        $data2 = [
            [
            'id' => NULL,
            'mage_order_id' => 'mage order id1'
            ],
            [
            'id' => NULL,
            'mage_order_id' => 'mage order id2'
            ]
        ];

        foreach($data as $item){
            $setup->getConnection()->insert($tableName,$item);
        }

        foreach($data2 as $item2){
            $setup->getConnection()->insert($tableName2,$item2);
        }

        $setup->endSetup();
    }
}

Note: Although id is defined as nullable=false you can still put value for id as NULL if it defined as primary key and autoincrement value.
Please Note also, that InstallData script only works on the first install of your module, so better uninstall and remove first the module on your setup_module table to make it work.

8
  • hi @magefrms, I need to insert 5 to 10 record for each table, need to define all of them? Commented Apr 2, 2019 at 5:58
  • I will update answer
    – fmsthird
    Commented Apr 2, 2019 at 5:59
  • you can do it like above
    – fmsthird
    Commented Apr 2, 2019 at 6:00
  • okay update me whenever you will encounter an error
    – fmsthird
    Commented Apr 2, 2019 at 6:01
  • 1
    okay sure, let me prepare file and check it, i have 20 rows for each tables Commented Apr 2, 2019 at 6:14

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.