Skip to content

Saving Customer Model directly causes loss of data #12479

@Danielc3

Description

@Danielc3

When saving a customer directly using the Customer Model custom attribute data is lost. This occurs when EAV caching is disabled. It appears you can get around this issue by using the Customer Repository or updateData function of the Customer Model. However, even though save is deprecated I would still expect it to work and looking into this has raised some concerns.

What I have found is that when saving directly some attribute set information is missing on the model, if we have a look at the aforementioned updateData function or the Customer Repository save function we see an acknowledgment of this issue in the form of a comment:

// Need to use attribute set or future updates can cause data loss

Below this comment both of these functions manually add an attribute set ID based on a hard-coded interface constant. I see no reason why this should not be done in the constructor, as that would get around this issue.

The saving process eventually leads to the checking of whether given attributes are in the expected attribute set using \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::isInSet. What gets passed in this circumstance is null, as the attribute set for the Customer Model is not set. This results in the function returning false and then the attribute value gets deleted.

With regards to why this only occurs with EAV caching disabled. When it is enabled attribute_set_info is not defined on the attributes (what happens is the attribute is cached and then attribute_set_info is added manually afterwards) which is another issue - and one I am yet to fully understand. With attribute_set_info not set isInSet actually returns true and therefore the attribute value is not cleared down.:

        if (!$this->hasAttributeSetInfo()) {
            return true;
        }

Preconditions

  1. Magento 2.2.0 or 2.1.7

Steps to reproduce

  1. Disable EAV cache.
  2. Create a custom attribute for Customer entity.
  3. Update a customer in the admin and add a value for this new attribute.
  1. Programmatically load the customer, make some change (e.g. firstname) and then save directly using the model.

Expected result

  1. When viewing the customer in the admin we will see the updated firstname and our previously saved custom attribute value.

Actual result

  1. The custom attribute value is lost.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fixed in 2.2.xThe issue has been fixed in 2.2 release lineFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions