Tag Archives: entity model class override

Magento Model override rules

I worked on various e-commerce open-source such x-cart, zen-cart but Magento is the special one for his great convention and high thought system. Override system was the great thinking of Magento team. Using Magento override rules you can can easily extends any Magento feature or can add new modules. Magento follows MVC pattern. Model is one of the main pattern of MVC. Usually model is used for database connectivity and for various logical coding. Mogento also use model for same purpose. Every module of Magento have his own model class. But you can extend or modify any existing model class by following Magento override rules.

Suppose, to fulfill your project requirements you need to change or enhance customer model class. You can change code in Mage_CUstomer_Model_Customer class. But it’s not a good practice of Magento customization and when upgrade your Magento version then there have a chance of remove your customized code. Means you will lost your changes. Really it will be pathetic for you !!!!! So we should follow magento override rules. Here i tried to clear how you can easily override your various model classes.

Override Customer Model class:

At first create new module to do override work and write below code in app/etc/modules/M4U_Customer.xml

<?xml version="1.0"?>
       <config>
             <modules>
                  <M4U_Customer>
                       <active>true</active>
                       <codePool>local</codePool>
                  </M4U_Customer>
            </modules>
     </config>

Do configuration in app/code/local/M4U/Customer/etc/config.xml

<?xml version="1.0"?>
    <config>
        <modules>
            <M4U_Customer>
                <version>0.1.0</version>
            </M4U_Customer>
        </modules>

        <global>
           <models>
              <customer>
                  <rewrite>
                      <customer>M4U_Customer_Model_Customer</customer>
                 </rewrite>
              </customer>
           </models>
        </global>
  </config>

Now write your new model class M4U_Customer_Model_Cutomer and define all override method
class M4U_Customer_Model_Customer extends Mage_Customer_Model_Customer
{
   // override existing method
   public function validate()
    {
       // Define new validate rules. From now magento call this validate method instead of existing method
        //return $errors;
        return true;
    }

    // You can create new method as you needed.
    public function newMethod()
    {
       // function logic
    }
}

Override Address Model class :

Every thing is same as customer model override system. Just put a line in etc/config.xml.

<?xml version="1.0"?>
    <config>
        <modules>
            <M4U_Customer>
                <version>0.1.0</version>
            </M4U_Customer>
        </modules>

        <global>
           <models>
              <customer>
                  <rewrite>
                      <customer>M4U_Customer_Model_Customer</customer>
                      <address>M4U_Customer_Model_Address</address>
                  </rewrite>
              </customer>
           </models>
        </global>
  </config>

Now write your new model class M4U_Customer_Model_Address and define all override method
class M4U_Customer_Model_Address extends Mage_Customer_Model_Address
{
   // override existing method
   public function validate()
    {
       // Define new validate rules. From now magento call this validate method instead of existing method
        //return $errors;
        return true;
    }

    // You can create new method as you needed.
    public function newMethod()
    {
       // function logic
    }
}

Override Customer Entity Model class :

Define app/code/M4U/Customer/etc/config.xml like below. Here you will get clear concept how to override various model class in same xml.

<?xml version="1.0"?>
    <config>
        <modules>
            <M4U_Customer>
                <version>0.1.0</version>
            </M4U_Customer>
        </modules>

        <global>
           <models>
              <customer>
                  <rewrite>
                      <customer>M4U_Customer_Model_Customer</customer>
                      <address>M4U_Customer_Model_Address</address>
                  </rewrite>
              </customer>
              <customer_entity>
                  <rewrite>
                      <address>M4U_Customer_Model_Entity_Address</address>
                  </rewrite>
              </customer_entity>
           </models>
        </global>
  </config>

class M4U_Customer_Model_Entity_Address extends Mage_Customer_Model_Entity_Address
{
    protected function _afterSave(Varien_Object $address)
    {
        // Write your code
    }
}

Now just try your self. If you face any problem then make a post. I will try to help you.