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

<config>
   <global>
      <events>
            <tarif_save_before>
                <observers>
                    <adin_marketplace>
                        <type>singleton</type>
                        <class>adin_marketplace/observer</class>
                        <method>adinMarketplaceTarifSaveBefore</method>
                    </adin_marketplace>
                </observers>
            </tarif_save_before>
       </event>
   </global>
/config>

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()) {
            $this->isObjectNew(true);
        }
        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.

Magento app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1180 error

You try to save a product and you get this error : Invalid argument supplied for foreach().
This was unexpected and you don’t understand why.

The problem is, that you are not allowed to save products from the frontend.
The origData property is not filled

public function setOrigData($key=null, $data=null)
{
    if (Mage::app()->getStore()->isAdmin()) {
        return parent::setOrigData($key, $data);
    }

    return $this;
}

So when you try to save the product, this error is raised.

What to do when you encounter this error ?

A quick fix is to add this before saving, on your controller for example.

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

A proper solution is to extend Mage_Catalog_Model_Product and replace setOrigData method

public function setOrigData($key = null, $data = null)
{
    if (is_null($key)) {
        $this->_origData = $this->_data;
    } else {
        $this->_origData[$key] = $data;
    }
    return $this;
}