Magento How to setup script


If you are in the business of creating your own custom extension (module) for magento, then chances are that at some point you will need your extension to be able to persist data to database, this is where the installation scripts come in place. Later on, when you upgrade your extension and decide to add few new tables to database or few new columns to database table that was initially created by the original installation extension you will need to look for upgrade script. Finally, if you want to set some initial default values in the database tables of your extension you will need to look for data scripts.


Step 1: Declare Module


Magento 1


File path: app/code/etc/modules/MageCheck_DBScript.xml

<?xml version='1.0'?> <config> <modules> <MageCheck_DBScript> <active>true</active> <codePool>community</codePool> </MageCheck_DBScript> </modules> </config>

Magento 2


File path: app/code/MageCheck/Tutorial/registration.php

<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'MageCheck_Tutorial', __DIR__ );

File path: app/code/MageCheck/Tutorial/etc/module.xml

<?xml version='1.0'?> <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:framework:Module/etc/module.xsd'> <module name='MageCheck_Tutorial' setup_version='1.0.2'/> </config>

Step 2: Create Configuration Files


Magento 1


File path: app/code/community/MageCheck/DBScript/etc/config.xml

<?xml version='1.0'?> <config> <modules> <MageCheck_DBScript> <version>3.1.5.6</version> </MageCheck_DBScript> </modules> <global> <models> <magecheck_dbscript> <class>MageCheck_DBScript_Model</class> <resourceModel>magecheck_dbscript_resource</resourceModel> </magecheck_dbscript> <magecheck_dbscript_resource> <class>MageCheck_DBScript_Model_Resource</class> <entities> <ticket> <table>magecheck_dbscript_ticket</table> </ticket> </entities> </magecheck_dbscript_resource> </models> <resources> <magecheck_dbscript_setup> <setup> <module>MageCheck_DBScript</module> </setup> </magecheck_dbscript_setup> </resources> </global> </config>

Magento 2


File path: app/code/MageCheck/Tutorial/etc/config.xml

<?xml version='1.0'?> <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:module:Magento_Store:etc/config.xsd'> <default> <tutorial> <general> <enable>1</enable> <display_text>Tutorial</display_text> </general> </tutorial> </default> </config>


Step 3: Configure Module and Create Functions


Magento 1


File path: app/code/community/MageCheck/DBScript/Model/Resource/Ticket.php

<?php class MageCheck_DBScript_Model_Resource_Ticket extends Mage_Core_Model_Resource_Db_Abstract { protected function _construct() { $this->_init('magecheck_dbscript/ticket', 'ticket_id'); } }

File path: app/code/community/MageCheck/DBScript/Model/Resource/Ticket/Collection.php

<?php class MageCheck_DBScript_Model_Resource_Ticket_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract { public function _construct() { $this->_init('magecheck_dbscript/ticket'); } }

Magento 2


To enable custom module manually in Magento 2, two commands needs to be executed from the shell:


php bin/magento module:enable MageCheck_Tutorial

php bin/magento setup:upgrade

First command adds your module to the modules list in app/etc/config.php. Second one installs your module

If module has setup scripts, they are executed and current module version is saved in setup_module table. Magento regulates what needs setup by comparing module versions with what it sees in that table. If module is installing for the first time, Install + Upgrade scripts are triggered. If module was installed before, but module version is increased, only Upgrade scripts are triggered.

To disable module, you will run:

php bin/magento module:disable MageCheck_Tutorial

This will flag module as disabled in config.php.To uninstall the module, you have to run the following command:

php bin/magento module:uninstall MageCheck_Tutorial

Step 4: Create InstallData


Magento 1


File path: app/code/community/MageCheck/DBScript/sql/ magecheck_dbscript_setup/install-3.1.5.6.php

<?php $installer = $this; $installer->startSetup(); $table = $installer->getConnection() ->newTable($installer->getTable('magecheck_dbscript/ticket')) ->addColumn('ticket_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array( 'identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true, ), 'Id') ->addColumn('title', Varien_Db_Ddl_Table::TYPE_VARCHAR, null, array( 'nullable' => false, ), 'Title') ->addColumn('description', Varien_Db_Ddl_Table::TYPE_TEXT, null, array( 'nullable' => false, ), 'Description'); $installer->getConnection()->createTable($table); $installer->endSetup();

Magento 2


File path: app/code/MageCheck/Tutorial/Setup/InstallData.php

namespace MageCheck\Tutorial\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('magecheck_tutorial') )->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(); }

Step 5: Create UpgradeData


Magento 1


Note: Do not forget to modify the version from config.xml file in order to upgrade your script.

File path: app/code/community/MageCheck/DBScript/sql/ magecheck_dbscript_setup/upgrade-3.1.5.6-3.2.0.1.php

<?php $installer = $this; $connection = $installer->getConnection(); $installer->startSetup(); $installer->getConnection() ->addColumn($installer->getTable('magecheck_dbscript/ticket'), 'created_at', array( 'type' => Varien_Db_Ddl_Table::TYPE_TIMESTAMP, 'nullable' => true, 'default' => null, 'comment' => 'Created At' ) ); $installer->endSetup();

Magento 2


File path: app/code/MageCheck/Tutorial/Setup/UpgradeData.php

namespace MageCheck\Custom\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) { $setup->startSetup(); if(!$context->getVersion()) { //no previous version found, installation, InstallSchema was just executed } if (version_compare($context->getVersion(), '1.0.1') < 0) { //code to upgrade to 1.0.1 } if (version_compare($context->getVersion(), '1.0.3') < 0) { //code to upgrade to 1.0.3 } $setup->endSetup(); } }

Extra Content (Magento 2 only)

Install or Upgrade Data

Data setup scripts contain entries module needs to insert into database. Attributes that come with Magento by default, 404 and other Cms pages, various default groups and roles, are all examples of data setup. Data setup is executed after Schema setup, they function in a similar fashion.

Recurring (post install)

Recurring script is executed after any module setup. The idea is that Module1 can do something after Module2 and Module3 are installed, if needed. The only example in current Magento 2 is Magento\Indexer\Setup\Recurring class where Magento_Indexer module checks for new defined indexers and adds them to indexer_state table.

Uninstall

This works only with modules installed with Composer. Magento 2 modules can have Uninstall script. For example:

namespace MageCheck\Tutorial\Setup; use Magento\Framework\Setup\UninstallInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\Setup\ModuleContextInterface; class Uninstall implements UninstallInterface { public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $setup->endSetup(); } }

This is the sample way to setup script in Magento!