Magento:Describe the basic concepts of models, resource models, and collections, and the relationship they have to one another

magento

Solution

Resource Models are objects that contain code which fetches data from a datastore. In practice, this means a resource model is the object which contains the SQL building and fetching code, as well as references to objects which connects to the main Magento database.

Models are objects which contain database agnostic code for interacting with a "type" of data. In traditional data modeling terms your model objects contain the business logic for a particular kind of object (kind of object meaning Product, Customer, etc.).

Important: In addition to the above definition, parts of Magento code use "Models" as generic object that contain business logic unrelated to data. These "models" should be thought of as plain old objects, just instantiated through Magento factory pattern. Models that inherit from `Mage_Core_Model_Abstract` are the former — models which do not are the later. This post assumes "model" refers to the former. I've also started referring to these as Magento's CRUD models.

A Collection is an object which contains code that fetches a group (collection, array, list, etc.) of model objects. Since it generates SQL to do this, it's also considered a resource model and is instantiated with the `Mage::getResourceModel` method, (Although collection objects inherits from a different chain of classes than the normal resource models. These are not the design patterns you are looking for). Collection objects also implement certain standard PHP interfaces, and may be used in `foreach` loops to iterate over their results.

A Magento model object contains a reference to a resource model, which is uses to load its data. There's an individual resource model object for each model object. i.e. A Product Model has a Product resource model.

A Magento model object may also be used to instantiate a collection object. Collection objects are also typed to match their model objects. A Product model may be used to instantiate a Product collection object.

A collection object creates SQL to fetch a group (collection, array, list, etc.) of objects, and it also contains code to assign data to the main model object. Because this code is slightly different than the code in a model's resource model object, there are often slight discrepancies between models loaded directly or via a collection. For example, a collection does not call each model's `_afterLoad` method, or an EAV collection will not load all attribute data by default (unless `addAttributeToCollection('*')` is used). A lot of Magento development is tracking down and account for these discrepancies.

Finally, there are places in the Magento source code that deviate from the above. For example: Report collection object are free standing — they are not ties to a specific model class. Keep the above in mind, but be ready for specific Magento modules to surprise you.

Problem

I'm trying to understand Magento and I was wondering if someone can give me any insight on this topic Describe the basic concepts of models, resource models, and collections, and the relationship they have to one another

Original source