I want to add a custom attribute to my customer. For now I have this : (on magento 2.2.4)
<?php
namespace My\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory, \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory)
{
 $this->customerSetupFactory = $customerSetupFactory;
 $this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
 $installer = $setup;
 $installer->startSetup();
 /** @var CustomerSetup $customerSetup */
 $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
 $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "customer_bonus");
 $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
 $attributeSetId = $customerEntity->getDefaultAttributeSetId();
 $attributeSet = $this->attributeSetFactory->create();
 $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
 $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_bonus', [
 'type' => 'varchar',
 'label' => 'Customer Bonus',
 'input' => 'text',
 'required' => false,
 'visible' => true,
 'user_defined' => true,
 'system' => false,
 ]);
 $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_bonus')
 ->addData([
 'attribute_set_id' => $attributeSetId,
 'attribute_group_id' => $attributeGroupId,
 'used_in_forms'=>['adminhtml_customer']
 ]);
 $attribute->save();
 $installer->endSetup();
}
}
But there's nothing on my backend, I want have something like this result : Magento 2 - How to add a custom column in customer grid
And CustomerSetupFactory is not find in PHP Storm. I tried
php bin/magento setup:di:compile
but nothing change.
Edit : the class is found with setup:di:compile dans after if I do setup:upgrade it's not found anymore.
- 
 Factories are service classes that instantiate non-injectable classes, that is, models that represent a database entity. They create a layer of abstraction between the ObjectManager and business code.Aditya Shah– Aditya Shah2018年05月25日 15:39:18 +00:00Commented May 25, 2018 at 15:39
- 
 they are an automatically generated class type.Aditya Shah– Aditya Shah2018年05月25日 15:39:21 +00:00Commented May 25, 2018 at 15:39
- 
 Please try reindeingAditya Shah– Aditya Shah2018年05月25日 15:39:47 +00:00Commented May 25, 2018 at 15:39
- 
 php bin/magento indexer:reindexAditya Shah– Aditya Shah2018年05月25日 15:39:52 +00:00Commented May 25, 2018 at 15:39
- 
 Reidexer didn't change it, do you have another idea ?Morgan– Morgan2018年05月25日 15:48:11 +00:00Commented May 25, 2018 at 15:48
1 Answer 1
class UpgradeData implements UpgradeDataInterface
 {
 /**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.NPathComplexity)
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 */
 protected $customerSetupFactory;
 private $attributeSetFactory;
public function __construct(
 \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory,
 AttributeSetFactory $attributeSetFactory) {
 $this->customerSetupFactory = $customerSetupFactory;
 $this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
 $setup->startSetup();
 /** @var CustomerSetup $customerSetup */
 if (version_compare($context->getVersion(), '1.0.1', '<')) {
 $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
 $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
 $attributeSetId = $customerEntity->getDefaultAttributeSetId();
 $attributeSetId = $customerEntity->getDefaultAttributeSetId();
 /** @var $attributeSet AttributeSet */
 $attributeSet = $this->attributeSetFactory->create();
 $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
 $customerSetup->addAttribute(Customer::ENTITY, 'attributename', [
 'type' => 'int',
 'backend' => '',
 'label' => 'Label',
 'input' => 'text',
 'source' => '',
 'visible' => true,
 'required' => false,
 'default' => '',
 'frontend' => '',
 'sort_order' => 1000,
 'position' => 1000,
 'unique' => true,
 'system' => 0,
 ] );
 //add attribute to attribute set
 $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'sap_id')
 ->addData([
 'attribute_set_id' => $attributeSetId,
 'attribute_group_id' => $attributeGroupId,
 'used_in_forms' => ['adminhtml_customer'],
 ]);
 $attribute->save();
 $setup->endSetup();
 } 
 }
}
This may help you out
- 
 Where does this class is suppose to go ?Morgan– Morgan2018年05月28日 07:28:12 +00:00Commented May 28, 2018 at 7:28
- 
 The install data or update data of setup scripts is useful to add a new attribute to the ui forms.Sairam Sigirisetty– Sairam Sigirisetty2018年05月28日 10:43:10 +00:00Commented May 28, 2018 at 10:43
Explore related questions
See similar questions with these tags.