When you install or upgrade a module, you may need to change the database structure or add some new data in current table. Magento 2 provide you some classes which you can do all of them.
- InstallSchema – This class will execute when the module is installed to setup the database structure.
- InstallData – This class will execute when the module is installed to initial the data for database table.
- UpgradeSchema – This class will execute when the module is upgraded to setup the database structure.
- UpgradeData – This class will execute when the module is upgraded to add/remove data from table.
Now we want to add new column in Magento Structure. For this we need to create the InstallSchema file.
Create the InstallSchema.php in the path app/code/[Vendor Name]/[Module Name]/Setup/InstallSchema.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
namespace [Vendor Name]\[Module Name]\Setup; use Magento\Framework\DB\Ddl\Table; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $connection = $setup->getConnection(); $column = [ 'type' => Table::TYPE_SMALLINT, 'length' => 6, 'nullable' => false, 'comment' => 'Gift Charge Enable', 'default' => '0' ]; $connection->addColumn($setup->getTable('sales_order'), 'giftcharge_enable', $column); } } |
After adding this column in the setup file, run the following command:
php bin/magento setup:upgrade
After installing the module, you can see the field in the Sales_order table.