为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕容森 2021-12-24 15:12:58
升级我的模块之一时,我在创建新的客户属性时遇到了麻烦。我已经/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根本没有被执行。
查看完整描述

1 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

在 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 中尝试为每个版本调用单独的函数。喜欢:



     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(在这种情况下无需检查版本)。


bin/magento setup:upgrade检查eav_attribute新属性表后。


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


查看完整回答
反对 回复 2021-12-24
  • 1 回答
  • 0 关注
  • 142 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号