Skip to content

Instantly share code, notes, and snippets.

@alexmorco
Last active April 12, 2018 12:55
Show Gist options
  • Save alexmorco/fad9015a6f260952b43e4b3b26da1248 to your computer and use it in GitHub Desktop.
Save alexmorco/fad9015a6f260952b43e4b3b26da1248 to your computer and use it in GitHub Desktop.
Setup Script For Magento 2 For Custom Module : https://www.cloudways.com/blog/setup-scripts-in-magento-2/

If you’re a Magento extension developer, there’s a chance that at some point you’ll want the extension to create and delete records from the database. You might even want to add more tables to the database or new columns to the table when you upgrade the module version. Such are the cases when Setup scripts can come in handy.

Setup scripts in Magento 2 are essential when writing custom modules. There are many differences between how these scripts are handled in Magento 2 and Magento 1. In Magento 2, we have 6 different types of Setup Scripts that can be used with our custom modules:

  • InstallSchema
  • UpgradeSchema
  • InstallData
  • UpgradeData
  • Recurring
  • Uninstall

In Magento 1, we have to create separate folders for Setup and Data scripts with the separate version numbering files. However, in Magento 2 this has been simplified. Now we only have to create a single folder called Setup in the root of the module directory and create only one file for each script. For the reference, all mentioned Setup Scripts are located at /setup/src/Magento/Setup/.

Setup scripts in Magento 2 are triggered using the Command Line Interface (CLI). When a Magento 2 module is first installed, an entry to the setup_module table is added for that particular module, using the setup_version attribute specified in the module.xml file that can be found in the Namespace/ModuleName/etc directory.

Install Schema and Install Data

As the names suggest, the InstallSchema.php and InstallData.php scripts run when the module is first installed in Magento 2. These scripts are used to modify the data and structure of the database respectively. It means, if InstallSchema.php and InstallData.php are created after the installation of the module, they will never be executed.

The example of an InstallSchema.php script looks like this:

<?php

 

namespace Namespace\ModuleName\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)

    {

        $installer = $setup;

        $installer->startSetup();

        $connection = $installer->getConnection();

        //cart table

        $connection->addColumn(

                $installer->getTable('quote_item'),

                'remarks',

                [

                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,

                    'length' => 255,

                    'comment' =>'Remarks'

                ]

            );

        $installer->endSetup();

    }

}

Notice that the InstallSchemaInterface interface has been implemented, and all of the setup code is placed within the install function. The example code uses DDL to add a column called remarks in the quote_item table.

The InstallData.php script uses a similar practice. However, it implements InstallDataInterface instead of InstallSchemaInterface. Take a look at this example:


<?php

 

namespace Namespace\ModuleName\Setup;

 

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();

        //Any Query

        //$setup->getConnection()->query("INSERT INTO .....");

        $setup->endSetup();

    }

}

Upgrade Schema and Upgrade Data

The UpgradeSchema.php and UpgradeData.php scripts help in updating the database data and its structure. Updating the module version in module.xml will allow the Upgrade scripts to be run when the magento setup:upgrade CLI command is executed.

Here’s an example of UpgradeSchema.php script:


<?php

 

namespace Namespace\ModuleName\Setup;

 

use Magento\Framework\Setup\UpgradeSchemaInterface;

use Magento\Framework\Setup\SchemaSetupInterface;

use Magento\Framework\Setup\ModuleContextInterface;

 

class UpgradeSchema implements UpgradeSchemaInterface

{

    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

    {

        if (version_compare($context->getVersion(), '1.0.1') < 0) {

        $installer = $setup;

        $installer->startSetup();

        $connection = $installer->getConnection();

        //Order address table

        $connection->addColumn(

                $installer->getTable('sales_order'),

                'remarks',

                [

                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,

                    'length' => 255,

                    'comment' =>'Remarks'

 

                ]

            );

        $installer->endSetup();

       }

    }

}

Get more here: https://www.cloudways.com/blog/setup-scripts-in-magento-2/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment