I have some problem to save custom customer entity attribute in database.
I've created a Patch to add this attribute to the customer entity. The attribute is shown correctly in the backend, but when I try to save it, the value doesn't save in the database.
My code is the following:
<?php
declare(strict_types=1);
namespace Ped\Referral\Setup\Patch\Data;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Setup\EavSetupFactory;
class AddCustomerReferralCodeAttribute implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* @var AttributeRepositoryInterface
*/
private $attributeRepository;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory,
AttributeRepositoryInterface $attributeRepository,
AttributeSetFactory $attributeSetFactory,
EavSetupFactory $eavSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeRepository = $attributeRepository;
$this->attributeSetFactory = $attributeSetFactory;
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
* @return DataPatchInterface|void
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\StateException
*/
public function apply()
{
$this->removeReferralCodeAttribute();
$this->addReferralCodeAttribute();
}
/**
* Remove referral_code customer attribute
*/
private function removeReferralCodeAttribute()
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->removeAttribute(Customer::ENTITY, 'referral_code');
}
/**
* Add referral_code customer attribute
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\StateException
*/
private function addReferralCodeAttribute()
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(
Customer::ENTITY,
'referral_code',
[
'type' => 'varchar',
'label' => 'Referral Code',
'input' => 'text',
'required' => false,
'sort_order' => 999,
'position' => 999,
'visible' => true,
'default' => '',
'user_defined' => true,
'system' => 0,
'unique' => true,
]
);
$referralCodeAttribute = $customerSetup->getEavConfig()
->getAttribute(Customer::ENTITY, 'referral_code')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
]);
$this->attributeRepository->save($referralCodeAttribute);
}
}
If I replace $this->attributeRepository->save($referralCodeAttribute);
with $referralCodeAttribute->save()
all works properly, but I don't want to use the save()
method of attribute entity, because is deprecated.
I know that now I should use the resource model/api interface to save attribute, but in both cases, the attribute is showing on the admin panel, but when I try to save, it doesn't work. Why?
How can I use the resource model/api interface correctly to save the custom attribute?
Magento\Eav\Api\AttributeSetRepositoryInterface
?