Magento create observers on custom entities

If you need to create an observer on your custom entities, just follow these few steps:

In this case, we will create an observer on the before save event of the tarif entity.

On your module config.xml file, declare your observer, this will bind your hook event to your class/method


tarif_save_before means your observer will trigger on the before save event of the tarif entity.
To find out which entity to use, you can add (temporary) a mage::log() on this class : app/code/core/Mage/Core/Model/Abstract.php

 protected function _beforeSave()
        if (!$this->getId()) {
        Mage::dispatchEvent('model_save_before', array('object'=>$this));
        Mage::log('entity to use: '.$this->_eventPrefix.'_save_before');
        Mage::dispatchEvent($this->_eventPrefix.'_save_before', $this->_getEventData());
        return $this;

adin_marketplace is my module.
adin_marketplace/observer class is the class file to use (app/code/local/Adin/Marketplace/Model/Observer.php )
adinMarketplaceTarifSaveBefore is the method inside my class.

Now, create your class and your method : app/code/local/Geophyle/Marketplace/Model/Observer.php

class Adin_Marketplace_Model_Observer {
public function adinMarketplaceTarifSaveBefore($observer)
    $event = $observer->getEvent();
    $tarif = $event->getTarif();
    //do some stuff...

And that’s all.
You change an .xml file, so don’t forget to clear your cache.

Leave a Reply