开发者

Viewing Magento model data changes on before/after save event?

It开发者_开发百科 should be simple:

  • Hook a MODEL_save_after event (or MODEL_save_before if that’s more appropriate).
  • Check getData() vs getOrigData() to see what changes the user has made.

In the customer_address model, edited through the backend, both save events are triggered twice. The first time customer_address_save_before is triggered, followed by customer_address_save_after.

In both cases getOrigData() and getData() are identical, except getData() has a new updated_at value, and has a store_id set (is this a bug?). So, the model doesn’t have the submitted data from the user yet. The events are both before entering user data or validation, so this is of no use.

customer_address_save_before is triggered, followed by customer_address_save_after a second time. This time (in both cases), getOrigData() is empty, and getData() now has all the submitted data from the user. So I can’t compare these events either! It appears this is after validation, saving, the lot!

Why the save process appears to occur twice? Magento v1.3.2.4.


I wound up hooking customer_address_save_before, and comparing the results to what was in the database like so:

<?php
customer_address_save_before_listener ($event)
{
  $address = $event->getCustomerAddress();

  $database_address = Mage::getModel('customer/address')->load($address->getId());
}
?>

And comparing the getData() returns from the two. There were three gotcha's that I came across:

  • Use getEntityTypeId() on $address and check it. Despite hooking 'customer_address_save_before', you also get OrderAddress models being sent to your listener (this seems wrong to me, but ah well).
  • Check for arrays in $address->getData() values. For example, 'street' is returned from the DB as a single string, while the address your listener is passed has this exploded on endlines.
  • The CustomerAddress your listener is passed has a 'store_id'. Even though CustomerAddress doesn't store 'store_id', and it doesn't get saved (or loaded from) the database.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜