繁体   English   中英

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

[英]Create customer attribute not working in magento 2

我正在尝试在magento 2.0.1上创建客户属性。 我已经在这里检查了所有已回答的问题,但是我的代码仍然无法正常工作。

这里我的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();


}
}

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

该属性未创建。

我的模块已注册,我正在将其用于其他用途,并且正在工作。

尝试在模块的根目录中找到Setup文件夹,也不要忘记在文件中更改命名空间

尝试使用模块版本清除db setup_module表中的行

尝试以这种方式创建属性:

$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