Understand Overriding in Magento: Models

In this series, we're exploring overriding features available in the Magento eCommerce system. In the previous article, we discussed how to override core block files using a custom module. Today, I'll extend it and demonstrate how you can override core model files using an XML-based configuration file.

Why Overriding Models?

In Magento, models play an important role as they implement most of the business logic of the core features. There are times when you need to change the flow or business logic in models to implement your custom features. 

It's tempting to go ahead and change the core files directly, which seems an easy and straightforward solution. Having said that, it's a bad practice, as it makes upgrading Magento really difficult. The more core files you modify directly, the more difficult it becomes for you to keep track of your changes and apply them during every version upgrade!

In this tutorial, we'll make a custom module to understand how model overriding works in Magento. In this custom module, we'll see how to override a category model class of the core "Catalog" module. Overriding works by setting up certain XML tags as per the conventions of Magento.

I assume that you're familiar with the basic module creation process in Magento. If not, here's a good article explaining the basics of custom module creation. Let's jump right in to the course of this tutorial!

A Glance at the Setup

Here's the list of files required for the desired setup:

  • app/etc/modules/Envato_All.xml: It's a file used to enable our custom module.
  • app/code/local/Envato/Catalog/etc/config.xml: It's a module configuration file in which we'll set up model class overriding using certain tags as per the Magento conventions.
  • app/code/local/Envato/Catalog/Model/Category.php: It's a model class of our custom module which will override the base model class.

Creating Files and Folders: Custom Module

First, we need to create a module enabler file. Create a file "app/etc/modules/Envato_All.xml" and paste the following contents in that file. We've used Envato as our module namespace and Catalog as our module name. It'll enable our "Catalog" module by default.

Next, we need to create a module configuration file. Create "app/code/local/Envato/Catalog/etc/config.xml" and paste the following contents in that file.

First, we've defined a module version number using the <version> tag. After that, the <catalog> and <rewrite> tags are used to inform the Magento overriding system that we're going to override a "model" of the "Catalog" core module.

Next, the <category> tag is used to define a model identity which will be overridden by the Envato_Catalog_Model_Category class. It's mapped to a model file "Category.php" under the "Model" directory of the Catalog module. An important thing to notice here is that we're following a directory structure similar to the core module. Although that's not strictly necessary, it's better than using a different directory structure, because it helps to maintain readability.

Finally, the only remaining thing is to define a model class Envato_Catalog_Model_Category. Let's create a model file "app/code/local/Envato/Catalog/Model/Category.php" and paste the following contents in that file.

We've defined an Envato_Catalog_Model_Category class which extends the core Mage_Catalog_Model_Category model class of the "Catalog" module. Thus, you can override every method of the base class and create new methods if necessary.

In the above example, the getProductCollection method is overridden so it'll be called instead of the method defined in the core model class! You can modify the model code as per your requirements. 

When you override any methods in models, you should make sure that the data type of the return value of that method matches with the data type of the base class method. Since model methods are called from several core modules, we should make sure that it doesn't break other features!

Although this is a very basic example, it serves the purpose of overriding the core model, and you can extend it as per your custom requirements.

Conclusion

In this tutorial, we learned how to override core model files in Magento using a custom module. In the next and last part of this overriding series, we'll see how to override core controller files. Don't hesitate to leave your comments below!

Tags:

Comments

Related Articles