2

I have an existing module with code like this:

$attribute = $customerUpgrade->getEavConfig()->getAttribute(Customer::ENTITY, 'my_custom_attribute')
    ->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => [],
    ]);

$attribute->save();

Because $attribute is an instance of AbstractModel, the save() function is deprecated. The recommendation from what I can tell is to use ResourceModel instead, but getAttribute() returns an AbstractModel type, and I can't find an easy way to use ResourceModel here.

What would be the correct (non-deprecated) way to add data to the custom attribute in this case?

  • Did you try using resource save()? – Sohel Rana Feb 17 at 16:11
  • I might be missing something, but the question is basically, since I get back an AbstractModel, how do I use it with ResourceModel save()? I can use getResource() for this, but it's deprecated for the same reason. – Ynhockey Feb 17 at 16:22
  • Check my answer – Sohel Rana Feb 17 at 16:58
1

Check following way, where I use resource save method:


<?php
namespace SR\MagentoCommunity\Setup;

use \Magento\Framework\Setup\UpgradeDataInterface;
use \Magento\Framework\Setup\ModuleContextInterface;
use \Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Config as EavConfig;
use Magento\Eav\Setup\EavSetup;
use Magento\Customer\Model\ResourceModel\Attribute as CustomerAttributeResourceModel;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * @var EavConfig
     */
    private $eavConfig;

    /**
     * @var EavSetup
     */
    private $eavSetup;

    /**
     * @var CustomerAttributeResourceModel
     */
    private $customerAttributeResourceModel;

    /**
     * UpgradeData constructor.
     * 
     * @param CustomerSetupFactory $customerSetupFactory
     * @param EavConfig $eavConfig
     * @param EavSetup $eavSetup
     * @param CustomerAttributeResourceModel $customerAttributeResourceModel
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        EavConfig $eavConfig,
        EavSetup $eavSetup,
        CustomerAttributeResourceModel $customerAttributeResourceModel
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->eavConfig = $eavConfig;
        $this->eavSetup = $eavSetup;
        $this->customerAttributeResourceModel = $customerAttributeResourceModel;
    }

    /**
     * Upgrade data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '2.0.1', '<')) {
            $this->createMobileAttribute($setup);
        }
    }

    /**
     * @param ModuleDataSetupInterface $setup
     */
    public function createMobileAttribute($setup)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        // Add new customer attribute
        $customerSetup->addAttribute(
            Customer::ENTITY,
            'mobile',
            [

                'label'                 => 'Mobile',
                'input'                 => 'text',
                'required'              => false,
                'sort_order'            => 1000,
                'position'              => 1000,
                'visible'               => true,
                'system'                => false,
                'is_used_in_grid'       => false,
                'is_visible_in_grid'    => false,
                'is_filterable_in_grid' => false,
                'is_searchable_in_grid' => false,
                'default'               => '0',
                'user_defined' => true,
            ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile');
        $attribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']);
        $this->customerAttributeResourceModel->save($attribute);

        $setup->endSetup();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.