Magenta Model Anatomy

Collections and Models are the bread and butter of everyday Magento development

Posted on March 27, 2016 in Magento

Magento Model Anatomy

Magenta Data Models are used to manipulate and access the data. The model layer is divided into two fundamental type, simple models and EAV, where:

  • Simple Model: These model implementations are simple mappings of one object to one table, meaning that our object attributes match each field and our table structure.

  • Entity Attribute Value Model(EAV): These type of models are used to describe entities with a dynamic number of attributes.

In addition to that, each Model type is formed by the following layers:

  • Model class: Here is where most of our business logic resides. Models are used to manipulate the data, but they don’t access it directly.

  • Resource Model class: Resource Models are used to interact with the database on behalf of our models. The are in charge of the actual CRUD operations

  • Model Collection class: Each Data Model has a collection class; collections are objects that hold a number of individual Magento Model instances

Magento Models don’t contain any logic for communicating with the database; they are database agnostic. This gives Magento the capacity to support different types of databases and platform.

It’s magic - methods

In Magento, there is no definition for any of the getter and setter methods for product attributes, such as color or manufacturer. It happens that the Magento ORM system is indeed using magic method. When try to call a method, which doesn’t actually exist in our corresponding class, PHP will look into each of the parent classes for a declaration of that method. If the method cannot be still found on any of the parent classes, it will use its last resort and try to use a __call() method, and if found, Magento will call the magic method, thus passing the requested method name and its arguments.

The Product model doesn’t have a __call() method defined, but it gets one from the Varien_Object class from which all Magento models inherit from. The inheritance tree for the Mage_Catalog_Model_Product class is given in the following level:


EAV Model

EAV stands for entity, attribute and value.

  • Entity: The entity represents the data items (object) inside Magento products, customers, categories, and orders. Each entity is stored in the database with a unique ID.

  • Attribute: These are our object properties. Instead of having one column per attributes on the product table, attributes are stored on separated set of tables.

  • Value: As the name implies, it is simply the value link to a particular attribute

This design pattern is the secret behind Magento’s flexibility and power, allowing entities to add and remove new properties without having to do any changes to the color or templates.

By default, Magento Community Edition currently has eight different types of EAV:

  • Customer

  • Customer address

  • Products

  • Product categories

  • Orders

  • Invoices

  • Credit memos

  • Shipments

Pros and Cons

Pros: Whereas model can be seen as a vertical way of growing our database (new attribute add more rows), the traditional model would involve a horizontal grow pattern (new attribute add more columns) that would result in a schema redesign every time new attributes are added.

Cons: All this flexibility and power is not free, and there is a price to pay; implementing the EAV model results in having our entity data distributed on a large number of tables. Another major downside of EAV is the loss of performance when retrieving large collections of EAV objects and an increase on database query complexity.

comments powered by Disqus