Create Tabel In Magento 2

Prerequisite for follow this tutorial [optional]

I already created a boilerplate module, which makes easy to follow this tutorial. It is optional, you can follow with your own module too.

  1. Download module repository from bitbucket
  2. Extract downloaded folder
  3. Copy Bilal folder into your <magento-root>/app/code/

After run the below commands to enable module

$ php bin/magento module:enable Bilal_Cruddemo
$ php bin/magento setup:upgrade

Once you’ve run the above, you should be able to access the following URL in your system

http://your-host/crud

and see the rendered text

Hello alert/index.phtml

Create Table

Whenever you want to create new table, you should write create table logic in InstallSchema.php

Bilal/Cruddemo/Setup/InstallSchema.php

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Bilal\Cruddemo\Setup;

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

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();

        /**
         * Create table 'crud_test_emp1'
         */
        $table = $installer->getConnection()->newTable(
            $installer->getTable('crud_test_emp1')
        )->addColumn(
            'id',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['identity' => true, 'nullable' => false, 'primary' => true],
            'primary key'
        )->addColumn(
            'name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => false],
            'Name of employee'
        )->addColumn(
            'age',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['nullable' => false, 'default' => '0'],
            'Age of employee'
        )->setComment(
            'crud test table'
        );

        $installer->getConnection()->createTable($table);

        $installer->endSetup();
    }
}

Delete record of the cruddemo module from the setup_module table.

DELETE FROM `setup_module` WHERE `module`='Bilal_Cruddemo';

InstallSchema.php only execute while run setup upgrade, while setup upgrade Magento look into setup_module table, whether the extension new or exist, if exist, it skips the InstallSchema execution that’s why we delete the records, now Magento think “it is new module so we need to execute InstallSchema.php”

run the below command

php bin/magento setup:upgrade

That’s it!

Check the database, we have our newly created table.

Please carry same source code to follow the next tutorial.

About Bilal

I'm a software programmer, I handle Magento2 modifications at any level.

View all posts by Bilal →

2 Comments on “Create Tabel In Magento 2”

Leave a Reply

Your email address will not be published. Required fields are marked *