Models and ResourceModels

September 28, 2009 at 12:10 pm 2 comments

AllMagento models extend the base Mage_Core_Model_Abstract class. This class helps any model save itself to the database in a straight property-to-column name manner.
The model, when saving, calls up its own resource singleton and passes itself ($this) to the resource’s save method. The resource is then scripted to collect any values from the model’s internal _data array and prepare an insert or an update statement with those values.

Model’s have a _getResource method which retrieves a previously setup resource.This resource is initialized by the init method using a resource name of the pattern module_name. The name portion of that pattern represents the final portion of a class name. The prefix of the class name is specified by a portion of the XML in the module’s etc/config.xml. If class names have the term Mysql4 in their names, they are generally straight model resources.

If the word Entity appears in the class name, then the resource is an EAV Entity. Let’s look at the Wishlist module as an example. In the config.xml of the wishlist modulewe see a resourceModel tag under themain definition of models. The value of this resourceModel tag points to another XML tag under the models tag. The class tag of this new definition specifies the class name prefix for any resource of this module. The following code shows how the sample XML file would produce a resource for the wishlist module.

$wish = Mage::getResourceSingleton(’wishlist/wishlist’);
echo get_class($wish);
//outputs: Mage_Wishlist_Model_Mysql4_Wishlist

<global>
<models>
<wishlist>
<class>Mage_Wishlist_Model</class>
<resourceModel>wishlist_mysql4</resourceModel>
</wishlist>
<wishlist_mysql4>
<class>Mage_Wishlist_Model_Mysql4</class>
<entities>
<wishlist>
<table>wishlist</table>
</wishlist>
<item>
<table>wishlist_item</table>
</item>
</entities>
</wishlist_mysql4>

Entry filed under: Magento Intermidiate. Tags: .

Embedding HTML in the Footer Basic Magento Directory Overview

2 Comments Add your own

  • 1. Meghna  |  July 28, 2011 at 3:53 am

    Very useful with basic information ..

    Very nice blog

    Reply
  • 2. Magento定制  |  August 5, 2011 at 2:48 am

    Just started with magento. Best amongst all resources available to explain the ‘model’ and ‘ResourceModel’ reltionship.
    Loads of Thanks!

    Reply

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 )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Categories

Archives

Top Posts

    Top Rated

    Blog Stats

    • 6,373 hits

     

    September 2009
    M T W T F S S
         
     123456
    78910111213
    14151617181920
    21222324252627
    282930  

    Follow

    Get every new post delivered to your Inbox.