2

I'm adding some custom logic in the admin when the customer is saved. I've created an observer on the customer_save_before event. I am using the following code to get the customer:

/** @var \Magento\Customer\Model\Customer $customer */
$customer = $observer->getEvent()->getCustomer();

While I was relying on $customer->getGroupId() to get the customer group ID for example I noticed some unexpected behaviour. It was returning the wrong value (it was returning id=1 but in the database the id=4).

Looking into the issue, I've noticed the getStoredData method on the \Magento\Framework\Model\AbstractModel (which is extended by \Magento\Customer\Model\Customer) which returns the correct data form the database.

I wonder why getData is not returning the correct data in this case. I see that \Magento\Framework\Model\AbstractModel::storedData is being updated with the \Magento\Framework\Model\AbstractModel::_data by \Magento\Framework\Model\AbstractModel::updateStoredData on certain events, but I wonder how reliable storedData is.

1
  • Did you find out? Does getOrigData also have the data needed?
    – Alex
    Jun 10, 2020 at 16:10

1 Answer 1

0

The difference between getData and getStoredData is at least, that getData gives you the current version of the model, and getStored is supposed to give the version which is currently in the database.

But: storedData does not seem to be available when the Model comes from an Collection-load. origData seems more reliable here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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