How to Create a Setup Script in Magento 2?

InstallSchema – this class will run when the module is installed to set up the database structure.

In this article, we will use the sample module MageChallenge_SetupData to create a demo table.

if you are not ready creating new module follow this link

Create a New Module Magento2

Create a new file app/code/MageChallenge/SetupData/Setup/InstallSchema.php

<?php
namespace MageChallenge\SetupData\Setup;

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

/**
 * Class InstallSchema
 * @package MageChallenge\SetupData\Setup
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @throws Zend_Db_Exception
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        $table = $installer->getConnection()->newTable(
            $installer->getTable('magechallenge_custom_data')
        )->addColumn(
            'id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            [ 'identity' => true, 'nullable' => false, 'primary' => true, 'unsigned' => true],
            'Entity ID'
        )->addColumn(
            'customer_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            50,
            [ 'nullable' => false],
            'Customer Id'
        )->addColumn(
            'ingredient_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            255,
            [ 'nullable' => false],
            'Ingredient Id'
        )->addColumn(
            'status',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            [ 'nullable' => false, 'default' => null ],
            'Status'
        )->addColumn(
            'created_date',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            [ 'nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
            'Created Date'
        )->addColumn(
            'updated_date',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            [ 'nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
            'Updated Date'
        );
        $installer->getConnection()->createTable($table);
        $installer->endSetup();
    }
}

Run php bin/magento setup:upgrade

Thank you. Have fun 🙂

Latest posts by Chathura Janaka Liyanage (see all)

4 thoughts on “How to Create a Setup Script in Magento 2?

  1. I am commenting to let you know of the fine encounter my friend’s daughter found reading your site. She figured out many issues, not to mention what it’s like to have a wonderful giving mindset to have men and women quite simply have an understanding of selected hard to do subject areas. You really exceeded our expectations. Many thanks for giving the insightful, safe, explanatory and as well as easy thoughts on the topic to Jane.

  2. Thank you for your entire labor on this web site. My mom take interest in participating in investigations and it is obvious why. My partner and i learn all regarding the lively tactic you produce simple things through your web site and in addition invigorate response from the others on the situation then our favorite princess is truly studying a whole lot. Take advantage of the rest of the year. You have been doing a superb job.

  3. A lot of thanks for all your efforts on this site. Gloria loves engaging in investigation and it’s easy to understand why. Almost all know all concerning the dynamic means you give important secrets on the web blog and as well encourage contribution from others on that concern plus our daughter is always discovering a lot. Have fun with the remaining portion of the new year. You’re the one conducting a good job.

  4. I needed to post you that little bit of observation to say thanks once again for your magnificent pointers you’ve featured in this article. It is quite remarkably generous of people like you to give openly just what a number of us might have marketed as an e book to end up making some dough for themselves, most notably given that you could possibly have done it if you ever decided. These points as well served like the fantastic way to fully grasp other people have similar eagerness much like my own to know lots more on the topic of this issue. I am certain there are millions of more fun opportunities in the future for individuals who looked at your blog post.

Leave a Reply