You will follow these steps to Create and Upgrade Database in Magento 2:
- Install Schema
- Install Data
- Upgrade Schema
- Upgrade Data
Magento 2 provides some classes in the directory app/code/[Namespace]/[ModuleName]/Setup to create or upgrade database.
- InstallSchema – setup database structure
- InstallData – initial the data for database table.
- UpgradeSchema – upgraded database structure
- UpgradeData – upgraded (add/remove) data from table.
Functions of these classes will be run when we install or upgrade module by this command line:
php bin/magento setup:upgrade
In this topic, we will use these classes to setup database for Magestore_
-
Install Schema
We use module vendor Magestore and module name is DataExample.
Create file: app/code/Magestore/DataExample/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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php namespace Magestore\DataExample\Setup; class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface { public function install( \Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context ){ $installer = $setup; $installer->startSetup(); $table = $installer->getConnection() ->newTable($installer->getTable('data_example')) ->addColumn( 'example_id', \Magento\Framework\Db\Ddl\Table::TYPE_INTEGER, null, ['identity' => true, 'nullable' => false, 'primary' => true, 'unsigned' => true], 'Example Id' )->addColumn( 'title', \Magento\Framework\Db\Ddl\Table::TYPE_TEXT, 255, ['nullable' => false], 'Example Title' )->addColumn( 'content', \Magento\Framework\Db\Ddl\Table::TYPE_TEXT, '2M', [], 'Example Content' )->addColumn( 'created_at', \Magento\Framework\Db\Ddl\Table::TYPE_TIMESTAMP, null, ['nullable' => false, 'default' => \Magento\Framework\Db\Ddl\Table::TIMESTAMP_INIT], 'Created At' ); $installer->getConnection()->createTable($table); $installer->endSetup(); } } ?> |
In this file, we created a table has name “data_example” with 4 columns: example_id, title, content, created_at with data types are interger, varchar, text, timestamp
You can review all data types in this file: \Magento\Framework\Db\Ddl\Table.php
-
Install Data
File: app/code/Magestore/DataExample/Setup/InstallData.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 |
<?php namespace Magestore\DataExample\Setup; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; class InstallData implements InstallDataInterface { protected $_exampleFactory; public function __construct(\Magestore\DataExample\Model\ExampleFactory $exampleFactory) { $this->_exampleFactory = $exampleFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $data = [ 'title' => "First example title", 'content' => "First Example content" ]; $example = $this->_exampleFactory->create(); $example->addData($data)->save(); } } |
In this file we insert a row with value for “title” and “content” columns to data_example table.
-
Upgrade Schema
File: app/code/Magestore/DataExample/Setup/UpgradeSchema.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace Magestore\DataExample\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 ) { $installer = $setup; $installer->startSetup(); if(version_compare($context->getVersion(), '1.0.1', '<')) { $installer->getConnection()->dropColumn( $installer->getTable('data_example'), 'created_at' ); } $installer->endSetup(); } } |
1 |
<span style="font-weight: 400;"> </span> |
Note: We have to check module version of in upgrade function. In this example we upgraded module from version 1.0.0 to 1.0.1. Drop created_at column from data_example table.
-
Upgrade Data
File: app/code/Magestore/DataExample/Setup/UpgradeData.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 27 |
<?php namespace Magestore\DataExample\Setup; use Magento\Framework\Setup\UpgradeDataInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\ModuleContextInterface; class UpgradeData implements UpgradeDataInterface { protected $_exampleFactory; public function __construct(\Magestore\DataExample\Model\ExampleFactory $exampleFactory) { $this->_exampleFactory = $exampleFactory; } public function upgrade( ModuleDataSetupInterface $setup, ModuleContextInterface $context ) { if ( version_compare($context->getVersion(), '1.0.1', '<' )) { $data = [ 'title' => "The second example title", 'content' => "The second example content" ]; $example = $this->_exampleFactory->create(); $example>addData($dalogta)->save(); } } } |
1 |
These are all steps to Create and Upgrade Database in Magento 2. Hope all you guys can learn magento 2 easier with our series lesson.
The 4 steps I mention above is the shortest process for you to Create and Upgrade Database in Magento 2. With this guide, you can manage the Database in Magento 2 easily.
Thank you for reading this post.