浏览文章
文章信息
Magento2 UpgradeSchema.php版本升级控制
16595
模块中的Setup/UpgradeSchema.php在开发中的版本控制很重要,具体控制一般采取以下做法
<?php /** * Copyright © 2017-2020 秋枫雁飞 (aiweline@qq.com). All rights reserved * * 欢迎来到Aiweline开发工作室! */ namespace Aiweline\Blog\Setup; use Magento\Framework\Setup\UpgradeSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; /** * Blog schema update */ class UpgradeSchema implements UpgradeSchemaInterface { const admin_attribute_FOCUS_ON = 'focus_on'; const admin_attribute_HEIGHT = 'height'; const admin_attribute_WEIGHT = 'weight'; const admin_attribute_CITY = 'city'; const admin_attribute_OPCCUPATION = 'occupation'; const admin_attribute_AVATAR = 'avatar'; const admin_attribute_INFO = 'info'; /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $setup->startSetup(); $version = $context->getVersion(); $connection = $setup->getConnection(); // 此处一般填写比较版本大于当前版本,这样设置有利于开发移植,只是个人习惯,可自行设置 if (version_compare($version, '3.1.0') < 0) { // 添加管理员信息 $table = $setup->getTable('admin_user'); $connection->addColumn( $table, self::admin_attribute_AVATAR, [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'nullable' => true, 'comment' => '头像' ] ); $connection->addColumn( $table, self::admin_attribute_FOCUS_ON, [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'nullable' => true, 'comment' => '关注数' ] ); $connection->addColumn( $table, self::admin_attribute_INFO, [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'nullable' => true, 'comment' => '简介' ] ); $connection->addColumn( $table, self::admin_attribute_CITY, [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'nullable' => true, 'comment' => '城市' ] ); $connection->addColumn( $table, self::admin_attribute_HEIGHT, [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 'nullable' => true, 'comment' => '身高' ] ); $connection->addColumn( $table, self::admin_attribute_WEIGHT, [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 'nullable' => true, 'comment' => '身高' ] ); $connection->addColumn( $table, self::admin_attribute_OPCCUPATION, [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'nullable' => true, 'comment' => '职位' ] ); } $setup->endSetup(); } }