创建客户属性在 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 文件夹,同时不要忘记在文件中更改命名空间

尝试使用您的模块版本

清理数据库中的行setup_moduletable

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

$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);
}