创建一个app\code\Sugarcode\Test\Setup\UpgradeSchema.php
并运行升级命令
当版本发生更改时,只需在module.xml中进行更改,如果条件与版本比较,则在UpgradeSchema.php中再添加一个
if (version_compare($context->getVersion(), '2.0.1', '<')) {
// Changes here.
}
因此,当您运行upgrade命令时,它将运行UpgradeSchema.php
文件,并根据该版本比较版本,然后执行代码
前
<?php
namespace Sugarcode\Test\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$tableName = $setup->getTable('testtable');
if (version_compare($context->getVersion(), '2.0.0') < 0) {
// Changes here.
}
if (version_compare($context->getVersion(), '2.0.1', '<')) {
// Changes here.
}
if (version_compare($context->getVersion(), '2.0.2', '<')) {
if ($setup->getConnection()->isTableExists($tableName) == true) {
$connection = $setup->getConnection();
/* $connection->addColumn(
$tableName,
'updated_at',
['type' => Table::TYPE_DATETIME,'nullable' => false, 'default' => '', 'afters' => 'created_at'],
'Updated At'
); */
$connection->changeColumn(
$tableName,
'summary',
'short_summary',
['type' => Table::TYPE_TEXT, 'nullable' => false, 'default' => ''],
'Short Summary'
);
// Changes here.
}
}
$setup->endSetup();
}
}
module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Sugarcode_Test" setup_version="2.0.2" schema_version="2.0.2" />
</config>
如果可行,请通过单击右符号来接受答案