magento2中的UpgradeSchema脚本-upgradeschema.php介绍

UpgradeSchema脚本用于创建新表或向现有表添加列。.

假设它在每次setup:upgrade上运行,其中setup_module.schema_version低于<VendorName>/<ModuleName>/etc/module.xml下的setup_version,

我们负责控制特定版本的代码。这通常是通过if-ed-version_compare方法来完成的。为了更好地演示这一点,让我们创建<MAGELICIOUS_DIR>/Core/Setup/UpgradeSchema.phpfile,其中包含以下内容:

use\ Magento\ Framework\ Setup\ UpgradeSchemaInterface;
use Magento\ Framework\ Setup\ ModuleContextInterface;
use Magento\ Framework\ Setup\ SchemaSetupInterface;
class UpgradeSchema implements UpgradeSchemaInterface {
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) {
        $setup - > startSetup();
        if (version_compare($context - > getVersion(), '2.0.2') < 0) {
            $this - > upgradeToVersionTwoZeroTwo($setup);
        }
        $setup - > endSetup();
    }
    private function upgradeToVersionTwoZeroTwo(SchemaSetupInterface $setup) {
        echo 'UpgradeSchema->upgradeToVersionTwoZeroTwo()'.PHP_EOL;
    }
}

这里的if-ed-version_compare如下:如果当前模块版本等于2.0.2,则执行upgradeToVersionTwoZeroTwo方法。

如果我们要发布模块的更新版本,我们需要正确地提升<VendorName>/<ModuleName>/etc/module.xml的setup_version,否则UpgradeSchema就没有多大意义。

同样,我们应该始终确保针对特定的模块版本,从而避免在每次版本更改时执行代码。

当涉及到UpgradeSchema脚本时,数据库适配器实例的以下方法以及前面提到的方法将是令人感兴趣的:dropColumn:从表中删除列

dropForeignKey:从表删除外键

dropIndex:从表上删除索引

dropTable:从数据库中删除表

ModifyColumn:修改列定义

本内容转载自maxiaoke.com,magento专区。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值