在本文中,我们将了解如何在Magento 2中安装和升级模块的sql脚本。安装或升级模块时,可能需要更改数据库结构或为当前表添加一些新数据。为此,Magento 2为您提供了一些可以完成所有课程的课程。
表中的内容
- InstallSchema / InstallData
- UpgradeSchema / UpgradeData
- 升级架构
- 升级数据
- 定期
- 卸载
安装,升级,卸载SQL脚本概述
- InstallSchema - 安装模块时将运行此类以设置数据库结构
- InstallData - 安装模块时将运行此类以初始化数据库表的数据。
- UpgradeSchema - 在升级模块以设置数据库结构时,将运行此类
- UpgradeData - 当升级模块以从表中添加/删除数据时,将运行此类。
- 定期
- 卸载
所有课程都将位于app/code/Vendor/Module/Setup
文件夹中。运行以下命令行时将运行模块安装/升级脚本:
php bin/magento setup:upgrade
在本文中,我们将使用示例模块Mageplaza_HelloWorld来创建一些演示表和数据。
安装脚本:InstallSchema和InstallData
InstallSchema和InstallData类将在模块安装期间运行。
magento 2中的InstallSchema 安装脚本将用于更改数据库架构(创建或更改数据库表)。这是创建mageplaza_helloworld_post
表的安装脚本:
文件: app/code/Mageplaza/HelloWorld/Setup/InstallSchema.php
<?php
namespace Mageplaza\HelloWorld\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();
if (!$installer->tableExists('mageplaza_helloworld_post')) {
$table = $installer->getConnection()->newTable(
$installer->getTable('mageplaza_helloworld_post')
)
->addColumn(
'post_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
[
'identity' => true,
'nullable' => false,
'primary' => true,
'unsigned' => true,
],
'Post ID'
)
->addColumn(
'name',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable => false'],
'Post Name'
)
->addColumn(
'url_key',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
[],
'Post URL Key'
)
->addColumn(
'post_content',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'64k',
[],
'Post Post Content'
)
->addColumn(
'tags',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
[],
'Post Tags'
)
->addColumn(
'status',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
1,
[],
'Post Status'
)
->addColumn(
'featured_image',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
[],
'Post Featured Image'
)
->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('Post Table');
$installer->getConnection()->createTable($table);
$installer->getConnection()->addIndex(
$installer->getTable('mageplaza_helloworld_post'),
$setup->getIdxName(
$installer->getTable('mageplaza_helloworld_post'),
['name', 'url_key', 'post_content', 'tags', 'featured_image'],
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
),
['name', 'url_key', 'post_content', 'tags', 'featured_image'],
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
);
}
$installer->endSetup();
}
}
查看此文件,我们将看到:
课程必须延伸 \Magento\Framework\Setup\InstallSchemaInterface
该类必须具有install()
2个参数SchemaSetupInterface
和方法ModuleContextInterface
。该SchemaSetupInterface
是设置对象,它提供了许多功能与数据库服务器进行交互。在ModuleContextInterface
只有1个方法getVersion()
将返回你的模块的当前版本。
在上面的示例中,我们创建了一个以mageplaza_helloworld_post
columns:命名的表post_id, name, post_content, created_at ...
。
InstallData
将在InstallSchema
类之后运行,以将数据添加到数据库表中。
文件: app/code/Mageplaza/HelloWorld/Setup/InstallData.php
<?php
namespace Mageplaza\HelloWorld\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
protected $_postFactory;
public function __construct(\Mageplaza\HelloWorld\Model\PostFactory $postFactory)
{
$this->_postFactory = $postFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$data = [
'name' => "How to Create SQL Setup Script in Magento 2",
'post_content' => "In this article, we will find out how to install and upgrade sql script for module in Magento 2. When you install or upgrade a module, you may need to change the database structure or add some new data for current table. To do this, Magento 2 provide you some classes which you can do all of them.",
'url_key' => '/magento-2-module-development/magento-2-how-to-create-sql-setup-script.html',
'tags' => 'magento 2,mageplaza helloworld',
'status' => 1
];
$post = $this->_postFactory->create();
$post->addData($data)->save();
}
}
这个类将具有相同的概念InstallSchema
。
升级脚本:UpgradeSchema和UpgradeData
安装或升级模块时,这两个文件都将运行。这些类与Install类不同,因为它们将在每次模块升级时运行。因此,我们将需要检查的属性setup_version
中module.xml
,在app/code/Mageplaza/HelloWorld/etc/
和每个版本分开脚本。
在这个例子中,我们将attrubute更改setup_version
为1.2.0
文件: app/code/Mageplaza/HelloWorld/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="Mageplaza_HelloWorld" setup_version="1.2.0">
</module>
</config>
升级架构:
文件: app/code/Mageplaza/HelloWorld/Setup/UpgradeSchema.php
<?php
namespace Mageplaza\HelloWorld\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.2.0', '<')) {
$installer->getConnection()->addColumn(
$installer->getTable( 'mageplaza_helloworld_post' ),
'test',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
'nullable' => true,
'length' => '12,4',
'comment' => 'test',
'after' => 'status'
]
);
}
$installer->endSetup();
}
}
在这个类中,我们使用upgrade()
每次升级模块时运行的方法。我们还必须比较版本以添加每个版本的脚本。
升级数据:
这与UpgradeSchema
班级相同
文件: app/code/Mageplaza/HelloWorld/Setup/UpgradeData.php
<?php
namespace Mageplaza\HelloWorld\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
class UpgradeData implements UpgradeDataInterface
{
protected $_postFactory;
public function __construct(\Mageplaza\HelloWorld\Model\PostFactory $postFactory)
{
$this->_postFactory = $postFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.2.0', '<')) {
$data = [
'name' => "Magento 2 Events",
'post_content' => "This article will talk about Events List in Magento 2. As you know, Magento 2 is using the events driven architecture which will help too much to extend the Magento functionality. We can understand this event as a kind of flag that rises when a specific situation happens. We will use an example module Mageplaza_HelloWorld to exercise this lesson.",
'url_key' => '/magento-2-module-development/magento-2-events.html',
'tags' => 'magento 2,mageplaza helloworld',
'status' => 1
];
$post = $this->_postFactory->create();
$post->addData($data)->save();
}
}
}
重复的脚本
循环脚本是一个脚本,每次php bin/magento setup:upgrade
运行命令行时都会在模块设置脚本之后运行。
此脚本的定义与InstallSchema
类相同,但类的名称不同。您可以在此查看此类的示例vendor/magento/module-indexer/Setup/Recurring.php
卸载脚本
Magento 2为我们提供了卸载模块功能,它将删除所有尚未安装的表,数据。这是这个类的例子:
文件: app/code/Mageplaza/HelloWorld/Setup/Uninstall.php
<?php
namespace Mageplaza\HelloWorld\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('mageplaza_helloworld_post'));
$installer->endSetup();
}
}