upgradeSchema在magento 2中不起作用



我想在现有表中添加新列。我创建了一个upgradeSchema.php文件。并在module.xml文件中更改了版本。但执行升级命令后,什么也没发生。这是我的代码

<?php 
namespace BridgeTradeuserSetup; 
use MagentoFrameworkSetupUpgradeSchemaInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupSchemaSetupInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* {@inheritdoc}
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) {
$installer = $setup;
$installer->startSetup();
if (version_compare($context->getVersion(), '3.0.1', '<')) {
$installer->getConnection()
->addColumn(
$installer->getTable('batchcode_entity'),
'status',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'length' => 50,
'nullable' => false,
'default' => 'Active',
'comment' => 'Batchcode status'
]
);
}
$installer->endSetup();
}
}  
?>

然后我运行以下命令:bin/magento setup:upgrade

bin/magento setup:static-content:deploy -f

bin/magento setup:di:compile

bin/magento cache:flush

嘿,试着使用这个方法,只需更改版本号和列名以匹配它,我现在就尝试了,它在中工作

<?php

namespace SimplifiedMagentoDatabaseSetup;

use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupSchemaSetupInterface;
use MagentoFrameworkSetupUpgradeSchemaInterface;
use MagentoFrameworkDbDdlTable;

class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* Upgrades DB schema for a module
*
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if(version_compare($context->getVersion(),'0.0.2','<'))
{
//if version of your project is lower than 0.0.2 then implement this
$setup->getConnection()->addColumn(
$setup->getTable('affiliate_member'),
'phonenumber',
['nullable' => false,'type' => Table::TYPE_TEXT,'comment' => 'Phone number column']
);
}
$setup->endSetup();
}
}

但请确保您的module.xml文件具有您在if condition:中编写的类似版本

<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="SimplifiedMagento_Database" setup_version="0.0.2">
</module>
</config>

最新更新