更新模块时添加新的自定义客户属性

升级我的模块之一时,我在创建新的客户属性时遇到了麻烦。


我已经/app/code/vendor/modulename/Setup/UpgradeData.php使用当前代码创建了 UpgradeData.php 文件:


namespace Ucs\CustomerAttribute\Setup;

use Magento\Customer\Model\Customer;

use Magento\Framework\Setup\ModuleDataSetupInterface;

use Magento\Framework\Setup\UpgradeDataInterface;

use Magento\Framework\Setup\ModuleContextInterface;

use Magento\Customer\Setup\CustomerSetupFactory;

class UpgradeData implements UpgradeDataInterface{


private $customerSetupFactory;


public function __construct(

    CustomerSetupFactory $customerSetupFactory

) {

    $this->customerSetupFactory = $customerSetupFactory;

}


/**

 * {@inheritdoc}

 */

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context){


    $setup->startSetup();


    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);


    if (version_compare($context->getVersion(), '1.0.6') < 0) {


        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'nome_azienda', [

            'type' => 'varchar',

            'label' => 'Nome azienda',

            'input' => 'text',

            'source' => '',

            'required' => false,

            'visible' => true,

            'position' => 333,

            'system' => false,

            'backend' => ''

        ]);


        $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'nome_azienda')

            ->addData(['used_in_forms' => [

                'adminhtml_customer',

                'adminhtml_checkout',

                'customer_account_create',

                'customer_account_edit'

            ]]);

        ]);


简而言之,它需要创建 2 个新的文本 (varchar) 属性。我module.xml有 setup_version="1.0.5"  schema_version="1.0.5"所以它应该输入version_compare条件并创建属性,但是,运行后php bin/magento setup:upgrade它不起作用。如果我在setup_module表中签入,则setup_version和 中schema_version的版本正确更改module.xml。看起来由于某种原因UpgradeData.php根本没有被执行。


慕容森
浏览 143回答 1
1回答

拉丁的传说

在 Ucs\CustomerAttribute\etc\module.xml 中将版本更改为 1.0.6然后替换if (version_compare($context->getVersion(), '1.0.6') < 0) {和if (version_compare($context->getVersion(), '1.0.6', '<')) {编辑:只是为了确定..我在 /app/code/vendor/modulename/Setup/UpgradeData.php 下创建了 UpgradeData.php 文件你的意思是 app/code/Ucs/CustomerAttribute/Setup/UpgradeData.php ?编辑2:我假设你的机构叫做 Ucs。这就是我问过它的原因,因为这就是你的模块命名空间的建议。这不是推荐的做法,但为了安装程序验证的目的,将命名空间更改为:命名空间 vendor\modulename\Setup;我推荐的是:创建一个新模块或找到 app/code/[YOURCOMPANYNAME]/Customer - 尝试协调 Magento 原生模块。通过这种方式,您可以更轻松地管理代码、设计并且 Magento 不需要为每个功能加载单独的模块。在 UpgradeData.php 中尝试为每个版本调用单独的函数。喜欢:&nbsp; &nbsp; &nbsp;if (version_compare($context->getVersion(), '1.0.1', '<')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->addCustomerMyAttribute();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;$setup->endSetup();然后在下面&nbsp;private function addCustomerMyAttribute($setup){// Your code goes here}如果它是 app/code/[YOURCOMPANYNAME] 中 Customer 模块的第一个版本,请记住创建 InstallData.php 插入 UpgradeData.php(在这种情况下无需检查版本)。bin/magento setup:upgrade检查eav_attribute新属性表后。如果它在那里,请记住 bin/magento indexer:reindex 以便它转到平面表。如果它不在那里。将 ```die(var_dump('I'm running'); 放在upgrade函数的开头。
打开App,查看更多内容
随时随地看视频慕课网APP