Codegento Who Let Mage Out Of The Cage?

21Feb/1111

Rewriting a Model Class

One of the best ways to extend the capabilities of the Magneto core is to "rewrite" their classes. If create a new instance of a model class the way you are supposed to (Mage::getModel('awesome/example)), Magento has built in a handy way of being able to "rewrite" your request from model A to model B.

Let's say that you have a business requirement to always show the phrase "loves Magento" after you display the customer name. To do this, you can find every place that outputs the customer name, OR change the way the customer name is gotten. For this example, we will do the latter.

First we need to create our new class:


class Super_Awesome_Model_Customer extends Mage_Customer_Model_Customer
{

    public function getName()
    {
    	$name = parent::getName();
        $name = $name . ' loves Magento';
        return $name;
    }

}

Next, we need a way to tell Magento to use OUR class instead of the one it normally does. So, all over the place in Magento, you will find: Mage::getModel('customer/customer'). This will use the config.xml in the Mage_Customer module to build the class name Mage_Customer_Model_Customer. To get it to use ours instead we will put a rewrite into the config.xml. You can put this rewrite into ANY config.xml, but we will put it in the Super_Awesome module.

 <global>
        <models>
            <customer>
            	<rewrite>
            		<customer>Super_Awesome_Model_Customer</customer>
            	</rewrite>
            </customer>
        </models>
  </global>
Mage::getModel('customer/customer')
                  |           |_ Is the <customer> tag inside the <rewrite> element.
                  |_ Is the <customer> tag outside the <rewrite> element.

So now when you make a call for Mage::getModel('customer/customer'), Magento will FIRST check if there is a rewrite for that class alias and will return an instance of the Super_Awesome_Model_Customer model instead of Mage_Customer_Model_Customer.

If you were trying to rewrite Mage_Customer_Model_Customer_Api to Super_Awesome_Model_Example_Of_Customer_Api, you would add the rewrite:

 <global>
        <models>
            <customer>
            	<rewrite>
            		<customer_api>Super_Awesome_Model_Example_Of_Customer_Api</customer_api>
            	</rewrite>
            </customer>
        </models>
  </global>

If you want to take a look at how Magento checks for rewrites, take a look in Mage_Core_Model_Config->getGroupedClassName():

        $config = $this->_xml->global->{$groupType.'s'}->{$group};

        if (isset($config->rewrite->$class)) {
            $className = (string)$config->rewrite->$class;
        } else {
            if (!empty($config)) {
                $className = $config->getClassName();
            }
            if (empty($className)) {
                $className = 'mage_'.$group.'_'.$groupType;
            }
            if (!empty($class)) {
                $className .= '_'.$class;
            }
            $className = uc_words($className);
        }

        $this->_classNameCache[$groupRootNode][$group][$class] = $className;
        return $className;

Important Note: You cannot rewrite a model class unless it is created by using the Mage::getModel(). So if you have a class like Mage_Customer_Model_Address that extends Mage_Customer_Model_Address_Abstract and you want to rewrite the Mage_Customer_Model_Address_Abstract class to do something different, you are out of luck on the rewrite option. You would need to copy Mage_Customer_Model_Address_Abstract down into your local code pool.

Posted by Ben Robie

Comments (11) Trackbacks (0)
  1. Very Usefull Informations!

    thkx for sharing

  2. Thanks..

  3. TX!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  4. Perfect – give a better glue how it works inside.

  5. What’s up, just wanted to say, I enjoyed this article.

    It was practical. Keep on posting!

  6. This article is actually a nice one it helps new the web people, who are wishing in favor of blogging.

  7. I was excited to find this website. I need to to
    thank you for ones time for this fantastic read!!

    I definitely savored every little bit of it and i also have you
    saved to fav to check out new things on your blog.

  8. I reɑԁ this article fully on thee topic of the resemblance of hottest and earlier technologies, it’s amaƶing article.


Leave a comment

(required)

Trackbacks are disabled.