简体   繁体   English

创建在magento 2中不起作用的客户属性

[英]Create customer attribute not working in magento 2

I'm trying to create a customer attribute on magento 2.0.1. 我正在尝试在magento 2.0.1上创建客户属性。 I have checked all the answered questions here but my code still does not work. 我已经在这里检查了所有已回答的问题,但是我的代码仍然无法正常工作。

Here my InstallData.php located in Mymodule\\Cus\\Setup 这里我的InstallData.php位于Mymodule \\ Cus \\ Setup

namespace Mymodule\Cus\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
     * @codeCoverageIgnore
   */
       class InstallData implements InstallDataInterface
 {

/**
 * @var CustomerSetupFactory
 */
protected $customerSetupFactory;

/**
 * @var AttributeSetFactory
 */
private $attributeSetFactory;

/**
 * @param CustomerSetupFactory $customerSetupFactory
 * @param AttributeSetFactory $attributeSetFactory
 */
public function __construct(
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
}


/**
 * {@inheritdoc}
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();

    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->addAttribute(Customer::ENTITY, 'test_att', [
        'type' => 'varchar',
        'label' => 'Test ATT',
        'input' => 'text',
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'sort_order' => 1000,
        'position' => 1000,
        'system' => 0,
    ]);

    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'test_att')
        ->addData(['attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address','adminhtml_checkout'],
        ]);

    $attribute->save();


}
}

When i run both : php bin\\magento setup:upgrade php bin\\magento setup:flush 当我同时运行两个命令时:php bin \\ magento setup:upgrade php bin \\ magento setup:flush

the attribute is not created. 该属性未创建。

My module is registrated and i'm using for others things and it's working. 我的模块已注册,我正在将其用于其他用途,并且正在工作。

Try to locate Setup folder in the root of module also do not forget change namespace in the file 尝试在模块的根目录中找到Setup文件夹,也不要忘记在文件中更改命名空间

Try to clean row in the db setup_module table with your module version 尝试使用模块版本清除db setup_module表中的行

Try to create attribute in this way: 尝试以这种方式创建属性:

$eavTable = $installer->getTable('needded table');

$columns = [
            'column_name' => [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => false,
                'comment' => '',
            ],
        ];
$connection = $installer->getConnection();

foreach ($columns as $name => $definition) {
    $connection->addColumn($eavTable, $name, $definition);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM