Overriding Block or Model in magento

Here I am going to explain some points which must be considered while overriding a Mage’s php class. I am going to explain it step by step with example as overriding a model which is Mage_Review_Model_Resource_Review and the function to which I am overriding is _afterSave().

  • For overriding a class, you should create a new module (which is identical to the name of the module to which we are overriding) in app/code/local/{company_name} folder. Here {company_name} could be anything which you think would be unique throughout your project just like core files name “Mage”.
  • The created module should be registered in app/etc/modules folder.
  •     <config>
            <modules>
                <Company_Review> <!-- same like "Mage_Review" -->
                    <active>true</active>
                    <codePool>local</codePool>
                </Company_Review>
            </modules>
        </config>
    
  • Keep the directory structure of the overriding class (a model or block) same as the core class’.
  • Keep the class name same as the core class name.

    For example: If you are planning to override Mage_Review_Model_Resource_Review class then your overriding class should replace the “Mage” with {company_name}.

  •    Mage_Review_Model_Resource_Review => Company_Review_Model_Resource_Review
    
  • In the php class file, extend our created class to the overridden class and include its path from “Mage” folder.

    For example,

  • <?php 
    include_once("Mage/Review/Model/Resource/Review.php");
    class Company_Review_Model_Resource_Review extends Mage_Review_Model_Resource_Review 
    {
        protected function _afterSave()  //overriding function
        {
            //code here
        }
    }
    
  • Register your classes in local/{company_name}/{module_name}/etc/config.xml file
  • <?xml version="1.0"?>
    <config>
        <modules>     <!-- Optional, but good practice to include this -->
            <Company_Review>
                <version>1.0</version>
            </Company_Review>
        </modules>
        <global>
            <models>  <!-- would be "blocks" if you are overriding a block class -->
            	<companyreview> <!-- should be unique.. to keep it simple just add your company name to the cores class reference name -->
            		<class>Efk_Review_Model</class>   <!-- register of main class -->
            	</companyreview>
                <review>   <!-- core file's module name -->
                    <rewrite>
                        <resource_review>Efk_Review_Model_Resource_Review</resource_review>
                    </rewrite>
                </review>
            </models>
        </global>
    </config>
    

    The above layout is happened as shown in the below image:
    magento 1

    Now everything seems all set but there could be some issues related to the above config.xml file.
    Observe the below section which is excerpt from the above config.xml file

    <review>   <!-- core file's class reference name -->
          <rewrite>
                 <resource_review>Efk_Review_Model_Resource_Review</resource_review>
          </rewrite>
    </review>
    

    In the above code, the class references name which I mentioned is “review” which I am thinking to be correct after visiting the original module’s config.xml. Lets once visit the original module’s config.xml file which in our example case should be in app/code/core/Mage/Review/etc/config.xml file. The excerpt code is shown below:

    <config>
        <!-- some other code -->
        <global>
            <models>
                <review>
                    <class>Mage_Review_Model</class>
                    <resourceModel>review_resource</resourceModel>
                </review>
                <review_resource>
                    <class>Mage_Review_Model_Resource</class>
    	<!-- some other code -->
                </review_resource>
            </models>
    </config>
    

    As you can see above, there seems to be two types of class references(I don’t know what exactly they are) one is “<review>” and the other one is “<review_resource>”. As in our example we are planning to override Mage_Review_Model_Resource_Review which should be referring from “<review_resource>” instead of “<review>”. So, our overriding modules config.xml will change something like this:

    <?xml version="1.0"?>
    <config>
        <modules>
            <Company_Review>
                <version>1.0</version>
            </Company_Review>
        </modules>
        <global>
            <models>
                <companyreview>
                    <class>Company_Review_Model</class>
                </companyreview>
                <review_resource>            	
                    <rewrite>
                        <review>Company_Review_Model_Resource_Review</review>
                    </rewrite>
                </review_resource>
            </models>
        </global>
    </config>
    

    So, the correct layout is based on the following image:

    magento 2

    Now we are done and everything is perfect but sometimes still there could be issues.

    Sometimes the function which we are overriding calls a function with same name available in the parent class. So, care must be taken to call the original class’s parent class with name instead of using parent php keyword.

    For example:

    In our case, if the _afterSave() function has a line return parent::_afterSave() (which doesn’t have in real code), then call the original class’s parent class with name like Mage_Core_Model_Resource_Db_Abstract in the overrided class function. This will make sure that the overridden class is not overridden again by the original class.

    If still you can’t see the changes then re-indexing and clearing the cache will help.

    Thanks for reading my blog. Let me know if I missed something by adding comments below :)

    About these ads

    One thought on “Overriding Block or Model in magento

    1. Hi KAMLEKAR,

      What would be the correct config file for /app/code/core/Mage/Core/Model/Resource/Store.php. I am going slightly insane.

      Which is Mage_Core_Model_Resource_Store. The config I have is:

      1.0

      Mycompany_Core_Model

      Mycompany_Core_Model_Resource_Store

      Thank you for any help.

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s