문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top