更新模块时添加新的自定义客户属性
Adding a new custom Customer attribute when updating a module
我在升级我的模块之一时无法创建新的客户属性。
我用当前代码在 /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.xml
有
setup_version="1.0.5" schema_version="1.0.5"
所以它应该进入 version_compare
条件并创建属性,但是,在 运行 php bin/magento setup:upgrade
之后它不起作用。如果我签入 setup_module
table,setup_version
和 schema_version
会随着 module.xml
中的版本正确更改。看起来由于某种原因 UpgradeData.php
根本没有执行。
在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', '<')) {
编辑:只是为了确定..
I've created the UpgradeData.php file under
/app/code/vendor/modulename/Setup/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] 中客户模块的第一个版本,请记住创建 UpgradeData.php 的 InstallData.php(在这种情况下无需检查版本)。
在 bin/magento setup:upgrade
之后检查 eav_attribute
table 新属性。
如果它在那里记得 bin/magento indexer:reindex 所以它会变平 table。如果它不存在。将 ```die(var_dump('I'm 运行'); 放在 upgrade
函数的开头。
我在升级我的模块之一时无法创建新的客户属性。
我用当前代码在 /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.xml
有
setup_version="1.0.5" schema_version="1.0.5"
所以它应该进入 version_compare
条件并创建属性,但是,在 运行 php bin/magento setup:upgrade
之后它不起作用。如果我签入 setup_module
table,setup_version
和 schema_version
会随着 module.xml
中的版本正确更改。看起来由于某种原因 UpgradeData.php
根本没有执行。
在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', '<')) {
编辑:只是为了确定..
I've created the UpgradeData.php file under /app/code/vendor/modulename/Setup/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] 中客户模块的第一个版本,请记住创建 UpgradeData.php 的 InstallData.php(在这种情况下无需检查版本)。
在
bin/magento setup:upgrade
之后检查eav_attribute
table 新属性。如果它在那里记得 bin/magento indexer:reindex 所以它会变平 table。如果它不存在。将 ```die(var_dump('I'm 运行'); 放在
upgrade
函数的开头。