【问题标题】:Adding a new custom Customer attribute when updating a module更新模块时添加新的自定义客户属性
【发布时间】:2019-09-03 11:39:37
【问题描述】:

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

我已经使用当前代码在/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'
            ]]);
        $attribute->save();


        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'codice_univoco', [
            'type' => 'varchar',
            'label' => 'Codice Univoco',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'codice_univoco')
            ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]]);
        $attribute->save();


    }
}
}

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

【问题讨论】:

    标签: php magento magento2 magento2.2


    【解决方案1】:

    在 Ucs\CustomerAttribute\etc\module.xml 中 将版本更改为 1.0.6

    然后替换

    if (version_compare($context-&gt;getVersion(), '1.0.6') &lt; 0) {

    if (version_compare($context-&gt;getVersion(), '1.0.6', '&lt;')) {

    编辑:只是为了确定.. by

    我在下面创建了 UpgradeData.php 文件 /app/code/vendor/modulename/Setup/UpgradeData.php

    你的意思是 app/code/Ucs/CustomerAttribute/Setup/UpgradeData.php ?

    编辑2:

    我假设您的代理机构名为 Ucs。这就是我问它的原因,因为这就是建议您的模块名称空间的原因。 这不是推荐的做法,但为了安装程序验证,将命名空间更改为: 命名空间供应商\模块名\设置;

    我推荐的是:

    1. 创建一个新模块或找到 app/code/[YOURCOMPANYNAME]/Customer - 尝试对应 Magento 本机模块。这样您可以更轻松地管理代码、设计,并且 Magento 不需要为每个功能加载单独的模块。

    2. 在 UpgradeData.php 中尝试为每个版本调用单独的函数。 喜欢:

    
         if (version_compare($context->getVersion(), '1.0.1', '<')) {
                    $this->addCustomerMyAttribute();
                }
         $setup->endSetup();
    

    然后在下面

     private function addCustomerMyAttribute($setup){
    // Your code goes here
    
    }
    

    如果它是 app/code/[YOURCOMPANYNAME] 中 Customer 模块的第一个版本,请记住创建 InstallData.php 安装的 UpgradeData.php(在这种情况下无需检查版本)。

    1. bin/magento setup:upgrade 之后检查eav_attribute 表中的新属性。

    2. 如果它存在,请记住 bin/magento indexer:reindex 以便它转到平面表。如果它不存在。把```die(var_dump('I'm running'); 放在upgrade函数的开头。

    【讨论】:

    • 尝试改变'if',没有任何改变(ofc也改变了module.xml中的setup_version和schema_version。即使删除'if'语句也没有改变至于UpgradeData.php的位置文件在 app/code/vendor/modulename/Setup/UpgradeData.php 因为我正在尝试更新我的模块,当然命名不是 Ucs/CustomerAttribute/
    • 我设法做到了,而无需从头开始创建新模块。在数据库中,我从 setup_module 选项卡中删除了该模块的行,在 InstallData.php 文件中添加了新的属性声明并运行 bin/magento setup:upgrade,这不是最佳解决方案,因为由于某种原因它仍然不执行 UpgradeData。 php 脚本(即使在修复了命名空间中的错误之后),但至少它可以工作!我将您的答案添加为解决方案,因为创建新模块的建议将 100% 起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多