Using event-observer pattern is simple yet powerful method to implode your own code into critical moments of application lifetime. Magento defines vast range of different events – just have a look at list of events for Magento 1.3.0 created by Branko Ajzele to get idea of various aspects of application flow you can amend just by using event-observer technique. However when dealing with models you might be disappointed by lack of on_create and on_update events. Thankfully these events are quite easy to implement with the use of core model_save_before and model_save_after events.
Below goes config file for MageDev_Events module which will trigger mentioned events.
app/code/local/MageDev/Events/etc/config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <?xml version="1.0"?> <config> <modules> <MageDev_Events> <version>0.0.1</version> </MageDev_Events> </modules> <global> <models> <events> <class>MageDev_Events_Model</class> </events> </models> <events> <model_save_before> <observers> <events_before_save> <type>singleton</type> <class>events/observer</class> <method>onBeforeSave</method> </events_before_save> </observers> </model_save_before> <model_save_after> <observers> <events_after_save> <type>singleton</type> <class>events/observer</class> <method>onAfterSave</method> </events_after_save> </observers> </model_save_after> <on_create> <observers> <events_on_create> <type>singleton</type> <class>events/observer</class> <method>onCreate</method> </events_on_create> </observers> </on_create> <on_update> <observers> <events_on_update> <type>singleton</type> <class>events/observer</class> <method>onUpdate</method> </events_on_update> </observers> </on_update> </events> </global> </config> |
In order to test newly created events I defined on_create and on_update observers, however these observers should be moved to separate module.
I wanted the on_create and on_update events to be triggered after object is saved. This way when handling events I am sure the object is already stored in database. I can also retrieve all object’s data. If for example I would trigger on_create event before save, in most cases I wouldn’t get primary key of this object.
To check whether the object is updated or created I try to get it’s primary key and query database to find out if the row with the same key already exists in the table. Because I defined singleton as event type, I can store this information directly in object. After save I trigger on_create or on_update event – depending on is_newly_created flag stored in the object data.
app/code/local/MageDev/Events/Model/Observer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?php class MageDev_Events_Model_Observer { public function onBeforeSave($obj) { $object = $obj->getEvent()->getObject(); if ($object->getId() && $object->getId() == $object->load($object->getId())->getId()) { $object->setIsNewlyCreated(false); } else { $object->setIsNewlyCreated(true); } } public function onAfterSave($obj) { $object = $obj->getEvent()->getObject(); if ($object->getIsNewlyCreated()) { Mage::dispatchEvent('on_create', array('object' => $object)); } else { Mage::dispatchEvent('on_update', array('object' => $object)); } } public function onCreate($obj) { $object = $obj->getEvent()->getObject(); Mage::log('Just created new object of class ' . get_class($object) . '. Object id: ' . $object->getId()); } public function onUpdate($obj) { $object = $obj->getEvent()->getObject(); Mage::log('Just updated object of class ' . get_class($object) . '. Object id: ' . $object->getId()); } } |
onCreate and onUpdate methods are defined just to check if newly created event is triggered.
Described method of firing on_create and on_update events should be used only when you really need to use these events with each modification of any object (for example to create system log). If you need this functionality for specific model I recommend to use event prefix of this particular class. For example if you want to send email notifications when new customer account is created, use ‘customer_save_before’ and ‘customer_save_after’ instead of ‘model_save_before’ and ‘model_save_after’.



hi,
i want to fire a some code any type of saving.
mean there is a code , which i want to fire after contact save or after order save or after product save.
is it possible with model_save_after ?
or anything else ?
@anand
Of course is possible, read the tutorial better above, you need to dispatch events in the controller where you are saving the contact :
Mage::dispatchEvent('contact_after_save', array('request'=>$this->getRequest(), 'contact'=>$this->getContact());
Then define the events you are waiting for in the config.xml file and which observer methods will fire up from the Observer class. And you can add whatever you want to the defined method.
@konrad
Thanks for the nice explanation, after 1 month mucking around with Magento, finally things start to make more sense and now i realize its power and flexibility.
I have a problem,
I want to send an email to admin when customer update his profile.
But GetId() function in onBeforeSave not exist.
error message : Call to a member function getId() on a non-object
I want to send mail with customer ID, firstname, lastname.
excuse me my english