Magento2: Customer Custom Attributes deleted upon update

Created on 30 May 2017  路  6Comments  路  Source: magento/magento2


Preconditions


  1. Magento Enterprise 2.1.4

Steps to reproduce

  1. Added Custom customer attribute in UpgradeData
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); 
         $attribute_values = array(
            ['code' => 'custom_attr_1', 'label' => 'XXXXX', 'input' => 'text'],
            ['code' => 'custom_attr_2', 'label'  => 'XXXX', 'input' => 'text']              
        );

        foreach ($attribute_values as $value)
        {
            if (!$customerSetup->getAttributeId(Customer::ENTITY, $value['code'])) 
            {
                $customerSetup->addAttribute(Customer::ENTITY, $value['code'], [
                    'type' => 'text',
                    'label' => $value['label'],
                    'input' => $value['input'],
                    'required' => false,
                    'visible' => false,
                    'position' => 120,
                    'system' => false                        
                ]);                
                $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $value['code'])
                ->addData([                    
                    'used_in_forms' => ['adminhtml_customer'],
                     'is_user_defined' => 1              
                ]);

                $attribute->save();
            } 
        }

These custom attribute has been saved in Database upon customer login.

  1. And on saving the data for these attributes used the below code.

         $data = array(
            'custom_attr_1' => 'XXXX',
            'custom_attr_2' => 'XXXXX'
                    );
         $customer_info    = $this->_customerFactory->create()->load(10);
         if ($customer_info->getId()) {             
        try {
            $customer_info->addData($data);               
            $customer_info->save();               
        }
        catch (Exception $e) {
            return false; 
        }
    }
    
  2. These customer custom attribute are saved in customer_entity_text table. On running magento setup:upgrade command(during code deployment on other environment), and customer login, the above code is called and the data from the customer_entity_text table is deleted.

  3. It doesn't happen when setup:upgrade is not run.

Expected result

  1. Data is updated in customer_entity_text table

Actual result

  1. Data (custom customer attribute) gets removed from customer_entity_text table on saving customer object.

Any suggestions please??

Most helpful comment

@veloraven You actually can add custom attributes to any core magento2 entity (ie customer) in the CE edition, using the underlying EAV mechanism.
For example, I've added a custom attribute to the Category entity.
Please clarify what you mean by

As customer custom attributes are available in Enterprise edition only we can not treat this issue as an issue in GitHub.

All 6 comments

@jayalakshmit
Following is code snippet from my own code that adds custom attributes to Category entity.
This snippet sits inside the foreach.
Sorry I can't give you access to the whole thing as its in my employers' private repo, but this code snippet is generic enough that I can share.
Hope it helps you.

if (!$group['id']) {
    $group['id'] = $categorySetup->getAttributeGroupId(
        $entityTypeId,
        $defaultAttributeSetId,
        $group['code']
    );
}
$attributeId = $categorySetup->getAttributeId($entityTypeId, $customAttribute);
if ($attributeId === false) {
    $categorySetup->addAttribute(
        $entityTypeId,
        $customAttribute,
        $config['addAttribute']
    );
    $attributeId = $categorySetup->getAttributeId($entityTypeId, $customAttribute);
}
$categorySetup->updateAttribute(
    $entityTypeId,
    $attributeId,
    $config['updateAttribute'],
    null,
    $config['updateAttribute']['sort_order']
);
$categorySetup->addAttributeToSet(
    $entityTypeId,
    $defaultAttributeSetId,
    $group['id'],
    $attributeId,
    $config['updateAttribute']['sort_order']
);
$categorySetup->addAttributeToGroup(
    $entityTypeId,
    $defaultAttributeSetId,
    $group['id'],
    $customAttribute,
    $config['updateAttribute']['sort_order']
);

@jayalakshmit thank you for your report.
Please report EE issues via the Support portal of your account or Partner portal if you are a partner reporting on behalf of a merchant.
Github is intended for Community edition reports given no account management for CE users. This will allow for proper tracking of issues at the account level. As customer custom attributes are available in Enterprise edition only we can not treat this issue as an issue in GitHub.

@veloraven You actually can add custom attributes to any core magento2 entity (ie customer) in the CE edition, using the underlying EAV mechanism.
For example, I've added a custom attribute to the Category entity.
Please clarify what you mean by

As customer custom attributes are available in Enterprise edition only we can not treat this issue as an issue in GitHub.

@rparsi-commer, @veloraven,
As far as I know, it's the admin UI for editing those custom attributes which is only present in EE (similar to the UI for product attributes in EE).

However, the issue does look more like a technical question rather than a bug report. Please keep in mind that it is expected to use the Community Forums or the Magento Stack Exchange if you are looking for some help or an advice regarding setup or development.
The GitHub issue tracker is intended for tracking technical issues (read: "bugs") of Magento CE only.

@korostii @veloraven
First, it's not communicated anywhere (as far as I know) that developers should use the forums
or the stack exchange instead of github issues page.

Second, github is primarily used by developers as its where we commit code, so by nature we first look at the issues list for information/help.
There needs to be clear indication (somewhere in github) of where developers should go.

Hi @rparsi-commer,
You must be new here. This whole issues' section at Magento's repository is pretty clear indication of where the Magento, Inc. would prefer the developers to go, if you get my meaning... silently, preferably.

Unfortunately, there's a "Report all bugs" link to GitHub Issues in the admin footer of every Magento2 installation, so you'll see quite a lot of users complaining here and just a few developers listening. I would agree that it's somewhat sad, but that's just the way it is.

Jokes aside, there was an announcement for this change and if you'd spend any time here, you'd see a lot of issues closed automatically with the generic "moved to forums" message.
Moreover, ISSUE_TEMPLATE.md contains a link to the guidelines, which (among other things) mentions the same:

please refer to the Community Forums or Magento Stack Exchange for technical questions

You should've seen that when creating this issue report, have you not?

Was this page helpful?
0 / 5 - 0 ratings