通过模块升级(UpgradeData.php)创建自定义客户属性

时间:2019-07-25 13:05:16

标签: php magento magento2 magento2.2

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

我已使用当前代码在/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之后它不起作用。即使删除了“ if”条件,也不会创建任何属性。如果我签入setup_module表,则setup_versionschema_versionmodule.xml中的版本正确更改。出于某种原因,看来UpgradeData.php根本没有执行(即使试图将字符串记录到日志文件中也没有执行任何操作)。 ..../var/log/下的其他日志文件未显示任何错误或警告。使用此模块,我已经用installData.php文件创建了其他自定义客户属性,完全没有问题。

我不知道该怎么办,无法真正理解我在做什么错

相关问题