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

时间:2018-07-24 11:57:42

标签: php magento2

我正在尝试在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设置:升级 php bin \ magento setup:flush

该属性未创建。

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

1 个答案:

答案 0 :(得分:1)

尝试在模块根目录中找到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);
}