Adding Magento Models, Resource Models, Collections

If you are looking to add custom data into Magento, you will need to understand how to go about adding Magento models, resource models, collections and combining them into a custom module.

As an example for the purpose of this article, let’s assume you will be adding a Store Information module.

Assuming you are aware how to add a module’s declaration file and have knowledge of Magento’s module file and folder structure, let’s begin.

In the module’s config.xml configuration file, we need to add the version of our module. The version number added will be important later on in this article.


<config>
    <modules>
        <Custom_Storeinfo>
            <version>1.0.0</version>
        </Custom_Storeinfo>
    </modules>
    ....
</config>

In the same file, declare the custom model class prefix and a resourceModel node. The resourceModel node will be used in the next step.

<config>
    ....
    <global>
        <models>
            <storeinfo>
                <class>Custom_Storeinfo_Model</class> <!-- This is the model prefix -->
                <resourceModel>custom_storeinfo_resource</resourceModel>
            </storeinfo>
        </models>
    </global>
</config>

Resource models in Magento are the models that interact with the database using CRUD operations, so now we need to configure the entity we are going to add to the database. Note how this information is configured within a pair of the resourceModel node names we specified previously.


<config>
    ....
    <global>
        <models>
            ....
            <custom_storeinfo_resource>
                <class>Custom_Storeinfo_Model_Resource</class>
                <entities>
                    <store>
                        <table>custom_storeinfo_store</table>
                    </store>
                </entities>
            </custom_storeinfo_resource>
        </models>
    </global>
</config>

Now that the model and resource model configuration has been set up, we need to create the model, resource model and collection PHP files.

The model file, Store.php, should reside in the the module’s Model directory as we have defined the model class prefix within our config.xml file.


<?php
class Custom_Storeinfo_Model_Store extends Mage_Core_Model_Abstract 
{
    public function _construct() {
        $this->_init('storeinfo/store');
    }
}

With the model class set up, you will be able to use retrieve an instance of the model using Mage::getModel().

Mage::getModel('storeinfo/store');

This can be seen in practice when you have added some data to the database.

We also have defined a resource model prefix, Custom_Storeinfo_Model_Resource, which is where the custom resource model shall be added.


<?php
class Custom_Storeinfo_Model_Resource_Store extends Mage_Core_Model_Resource_Db_Abstract 
{
    public function _construct() {
        $this->_init('storeinfo/store', 'store_id');
    }
}

And lastly, the collection should be defined.


<?php
class Custom_Storeinfo_Model_Resource_Store_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract 
{
    public function _construct() {
        $this->_init('storeinfo/store');
    }
}

There is still one more step to cover. We haven’t actually told Magento what columns we are planning to add to our database table. Although we have declared the name of our table in config.xml (within the <table> nodes), we haven’t told Magento that we’d like to create this table, so let’s do that now.


<config>
    ....
    <global>
        ....
        <resources>
            <custom_storeinfo_setup>
                <setup>
                    <module>Custom_Storeinfo</module>
                </setup>
            </custom_storeinfo_setup>
        </resources>
    </global>
</config>

So the step here tells Magento that we are looking to add the table data in app/code/local/Custom/Storeinfo/sql/custom_storeinfo_setup.

It’s important to take note of the classes that the Magento models, resource models and collections extend.

  • Models extend Mage_Core_Model_Abstract
  • Resource Models extend Mage_Core_Model_Resource_Db_Abstract
  • Collections extend Mage_Core_Model_Resource_Db_Collection_Abstract

To add the data into Magento, please see the install and upgrade scripts article to continue on with this example.

Note: This article is based on Magento Community/Open Source version 1.9.