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
Models in Magento are an inherent part of the MVC (Model-View-Controller) architecture. Data operations, such as Create, Read, Update, and Delete on a database, are carried out using models. Magento's “Model system” is divided into three parts – models, resource models, and collections.
What is a Model in Magento 2? A Model is a class that represents an entity or a record from a database. The model also provides access to entity data that is used across the Magento 2 application. The model term comes from a Model-View-Controller design pattern and is an important component to understand in Magento 2.
Magento uses an active record pattern strategy for persistence. In this system, the model object contains a resource model that maps an object to one or more database rows. A resource model is responsible for performing functions such as: Executing all CRUD (create, read, update, delete) requests.
Collections in Magento are extremely useful, they allow you to retrieve objects such as products, categories, customers, etc. Retrieving these objects in a collection is a simple case of instantiating an instance of the object entity's factory class and using the model's 'getCollection' method, as follows.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With