If you're reading the Magento 2 devdocs for custom attributes, you'll probably notice they give an example of creating a new custom attribute like so:
<?php
namespace Magento\Customer\Setup\Patch\Data;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
class AddCustomerExampleAttribute implements DataPatchInterface
{
private $moduleDataSetup;
private $customerSetupFactory;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
}
public function apply()
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerSetup->addAttribute(Customer::ENTITY, 'attribute_code', [
// Attribute options (list of options can be found below)
]);
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [
UpdateIdentifierCustomerAttributesVisibility::class,
];
}
public function getAliases()
{
return [];
}
}
but right above is a notice:
Both the save() and getResource() methods for Magento\Framework\Model\AbstractModel have been marked as @deprecated since 2.1 and should no longer be used.
But they give no example of what to do instead (probably because the core Magento code still uses both methods AND if you've ever used Magento U, you'll see their sample code doesn't follow their own rules).
So, since the example above is setting up a Customer attribute, to save you can do the following:
<?php
namespace Magento\Customer\Setup\Patch\Data;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\ResourceModel\Attribute as AttributeResourceModel; // ADD ME
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
class AddCustomerExampleAttribute implements DataPatchInterface
{
private $moduleDataSetup;
private $customerSetupFactory;
private $attributeResourceModel;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory,
AttributeResourceModel $attributeResourceModel
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeResourceModel = $attributeResourceModel;
}
public function apply()
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerSetup->addAttribute(Customer::ENTITY, 'attribute_code', [
// Attribute options (list of options can be found below)
]);
}
$attribute = $customerSetup->getEavConfig()
->getAttribute(Customer::ENTITY, 'attribute_code')
->setData('used_in_forms', ['adminhtml_customer']); // additional data you want to set, like used_in_forms
// normally here is where you'd just do `->save()` but you'll notice your IDE will flag the save method as deprecated
// so instead:
$this->attributeResourceModel->save($attribute);
$this->moduleDataSetup->endSetup();
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [
UpdateIdentifierCustomerAttributesVisibility::class,
];
}
public function getAliases()
{
return [];
}
}