I have written the following script to create a custom customer attribute in Magento 2.2.3. When I run setup:upgrade
magento tells me data was installed but the attribute does not appear in eav_attribute
table.
<?php
namespace MyNameSpace\StoreName\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
{
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'store_real_name',
[
'type' => 'varchar',
'label' => 'Store Name',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
$storeName = $this->eavConfig->getAttribute(Customer::ENTITY, 'store_real_name');
$storeName->setData(
'used_in_forms',
['adminhtml_customer', 'customer_account_edit']
);
$storeName->save();
}
}
I have used an identical script to create other attributes without issue, but for some reason this one won't save. I have tried to disable and enable the module and re-run setup:upgrade
but nothing seems to work.
php bin/magento setup:upgrade
,php bin/magento setup:static-content:deploy