Magento 2中使用安装/升级脚本创建数据库表
如果要在Magento 2中创建新的自定义模块,在某些情况下,您可能需要创建新的数据库表,修改现有的数据库表,并且可能需要在数据库表中设置一些初始缺省值。您可以通过在Magento 2中创建安装/升级脚本来实现此目的。
以下是安装/升级SQL脚本的列表
- InstallSchema
- InstallData
- UpgradeSchema
- UpgradeData
- Recurring
- Uninstall
所有这些类都将位于app/code/
示例文件路径:app/code/Alwayly/CustomScript/Setup
在CLI中运行以下命令时,将运行模块的安装/升级脚本:
php bin/magento setup:upgrade
InstallSchema:
InstallSchema用于更改数据库模式,这意味着创建新数据库表或修改现有数据库表结构。InstallSchema类将在模块安装期间运行。
示例文件路径:app/code/Alwayly/CustomScript/Setup/InstallSchema.php
<?php namespace Alwayly\CustomScript\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(); if (!$installer->tableExists('comments')) { $table = $installer->getConnection() ->newTable($installer->getTable('comments')) ->addColumn( 'comment_id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, null, [ 'identity' => true, 'nullable' => false, 'primary' => true, 'unsigned' => true, ], 'ID' ) ->addColumn( 'name', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255, ['nullable => false'], 'Name' ) ->addColumn( 'email', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255, [], 'Email' ) ->addColumn( 'url_key', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255, [], 'URL Key' ) ->addColumn( 'message', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, '18K', [], 'Message' ) ->addColumn( 'status', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 1, [], 'Comment Status' ) ->addColumn( 'created_at', \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP, null, ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT], 'Created At' )->addColumn( 'updated_at', \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP, null, ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE], 'Updated At') ->setComment('Comments Table'); $installer->getConnection()->createTable($table); $installer->getConnection()->addIndex( $installer->getTable('comments'), $setup->getIdxName( $installer->getTable('comments'), ['name', 'email', 'url_key', 'message'], \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT ), ['name', 'email', 'url_key', 'message'], \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT ); } $installer->endSetup(); } }
InstallSchema安装程序类文件必须继承\Magento\Framework\Setup\InstallSchemaInterface并且必须具有一个带有2个参数SchemaSetupInterface和ModuleContextInterface的公共方法install()。
- SchemaSetupInterface:它是一个设置对象,它提供了与数据库服务器交互的机制。
- ModuleContextInterface:它有一个名为getVersion()的方法,用于获取模块的当前版本。
InstallData:
InstallData用于将数据添加到数据库表。InstallData类也将在模块安装期间运行。但它将在InstallSchema类之后运行。
示例文件路径:app/code/Alwayly/CustomScript/Setup/InstallData.php
UpgradeSchema:
UpgradeSchema用于创建或修改现有模块的数据库表。安装脚本和升级脚本之间的主要区别在于,安装脚本将在模块首次安装期间运行。但升级脚本将在每次模块升级时运行。
示例文件路径:app/code/Alwayly/CustomScript/Setup/UpgradeSchema.php
startSetup(); if(version_compare($context->getVersion(), '1.0.1', '<')) { $installer->getConnection()->addColumn( $installer->getTable( 'comments' ), 'user_image', [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'nullable' => true, 'length' => '254', 'comment' => 'Image', 'after' => 'url_key' ] ); } $installer->endSetup(); } }
UpgradeSchema安装程序类文件必须继承\Magento\Framework\Setup\UpgradeSchemaInterface, 并且必须具有一个带有2个参数SchemaSetupInterface和ModuleContextInterface的公共方法upgrade()。
UpgradeData:
UpgradeData用于添加/删除数据库表中的数据。这与UpgradeSchema类相同。
示例文件路径:app/code/Alwayly/CustomScript/Setup/UpgradeData.php
<?php namespace Alwayly\CustomScript\Setup; use Magento\Framework\Setup\UpgradeDataInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\ModuleContextInterface; class UpgradeData implements UpgradeDataInterface { protected $_commentFactory; public function __construct(\Alwayly\CustomScript\Model\CommentFactory $commentFactory) { $this->_commentFactory = $commentFactory; } public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { if (version_compare($context->getVersion(), '1.0.1', '<')) { $data = [ 'name' => 'Thomus Raj', 'email' => 'roni_cost@xample.com', 'url_key' => 'https://www.360magento.com', 'message' => 'My first comment', 'status' => 1 ]; $post = $this->_commentFactory->create(); $post->addData($data)->save(); } } }
Uninstall:
示例文件路径:app/code/Alwayly/CustomScript/Setup/Uninstall.php
<?php namespace Alwayly\CustomScript\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) { $installer = $setup; $installer->startSetup(); $installer->getConnection()->dropTable($installer->getTable('comments')); $installer->endSetup(); } }
希望这可以帮助你。