2

I have InstallSchema.php as follows:

<?php
namespace Namespace\Module\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;



use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $table = $setup->getConnection()->newTable(
            $setup->getTable('custom_table')
        )->addColumn(
            'custom_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Custom Id'
        )->addColumn(
            'name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            [],
            'Custom Name'
        )->setComment(
            'Custom Table'
        );
        $setup->getConnection()->createTable($table);

        $setup->endSetup();
    }
}

InstallData.php as follows:

<?php

namespace Namespace\Module\Setup;

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

class InstallData implements InstallDataInterface
{

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $tableName = $setup->getTable('custom_table');

            $data = [
                [
                    'name' => 'Content of the first post.',
                ],
                [
                    'name' => 'Content of the second post.',
                ],
            ];

            $setup
                ->getConnection()
                ->insertMultiple($tableName, $data);

        $setup->endSetup();
    }
}

I have executed php bin\magento setup:upgrade at time of installing.

What I found was Table with defined Schema was created but no data was inserted.

Please help what I am missing here.

7
  • Returning any error?
    – Bojjaiah
    Commented Oct 5, 2017 at 7:17
  • it's not returning any kind of error.
    – DEEP JOSHI
    Commented Oct 5, 2017 at 7:19
  • try to remove your module in setup_module table and re run the commands setup:upgrade.
    – Bojjaiah
    Commented Oct 5, 2017 at 7:21
  • I tried that but not working.
    – DEEP JOSHI
    Commented Oct 5, 2017 at 7:22
  • Post full of code, we will look out.
    – Bojjaiah
    Commented Oct 5, 2017 at 7:24

2 Answers 2

0

Please try with the below code with sample example: Module Name:- Stackexchange_Test

Path:- app/code/Stackexchange/Test/Setup/InstallSchema.php

  <?php
namespace Stackexchange\Test\Setup;

class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface
{
    /**
     * install tables
     *
     * @param \Magento\Framework\Setup\SchemaSetupInterface $setup
     * @param \Magento\Framework\Setup\ModuleContextInterface $context
     * @return void
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        if (!$installer->tableExists('custom_table')) {
            $table = $installer->getConnection()->newTable(
                $installer->getTable('custom_table')
            )
            ->addColumn(
                'rule_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                [
                    'identity' => true,
                    'nullable' => false,
                    'primary'  => true,
                    'unsigned' => true,
                ],
                'Rule Id'
            )
            ->addColumn(
                'rulename',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable => false'],
                'Rule Name'
            )
            ->addColumn(
                'description',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                [],
                'Rule Description'
            )
            ->addColumn(
                'status',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                1,
                [],
                'Rule Status'
            )
            ->addColumn(
                'created_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [],
                'Rule Created At'
            )
            ->addColumn(
                'updated_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [],
                'Rule Updated At'
            )
            ->setComment('Test Table');
            $installer->getConnection()->createTable($table);


        }
        $installer->endSetup();
    }
}
7
  • I am able to create Schema but no Data was inserted.
    – DEEP JOSHI
    Commented Oct 5, 2017 at 7:09
  • ok you are able to create table with columns right? Commented Oct 5, 2017 at 7:10
  • Please confirm whether you are able to insert the data directly from phpmyadmin end or using Magento2 programatically? Commented Oct 5, 2017 at 7:11
  • I am unable to insert data using InstallData.php in magento2.
    – DEEP JOSHI
    Commented Oct 5, 2017 at 7:13
  • Ok i do understand let me check and getback to u thanks Commented Oct 5, 2017 at 7:16
0

I just copy pasted in my custom module it's working fine. See below code and screenshot.

InstallSchema.php

<?php
namespace Learning\Deep\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $table = $setup->getConnection()->newTable(
            $setup->getTable('custom_table')
        )->addColumn(
                'custom_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'Custom Id'
            )->addColumn(
                'name',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                [],
                'Custom Name'
            )->setComment(
                'Custom Table'
            );
        $setup->getConnection()->createTable($table);

        $setup->endSetup();
    }
}

InstallData.php

<?php

namespace Learning\Deep\Setup;

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

class InstallData implements InstallDataInterface
{

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $tableName = $setup->getTable('custom_table');

        $data = [
            [
                'name' => 'Content of the first post.',
            ],
            [
                'name' => 'Content of the second post.',
            ],
        ];

        $setup->getConnection()->insertMultiple($tableName, $data);

        $setup->endSetup();
    }
}

After that run php bin/magento setup:upgrade command, then go to phpmyadmin see the table of data.

For me, it's worked see below pic.

enter image description here

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.