In this post, we will see what are Models and how to use them.
What is a model?
A model is a class that represents database table in an object oriented way. It can also be used to perform business logic.
Layers of Model in Magento
Magento uses three layer approach to models.
- Entity Model
This type of model provides basic functionalities like create, read, update and delete operations. They may also contain other business logic. They do not contain any logic to connect or query the database. - Resource Model
This type of model actually talks to database. Entity models actually talk to database with the help of resource models. Hence we can say that every entity model has a corresponding resource model.
The advantage of having separate layers of actual model and database model is that when you wish to change your database, you just have to replace your resource model and hence, your application is untouched. - Collections
Each Magento model type has a unique collection object associated to it. This helps in returning list of objects.
Type of Models in Magento
There are three type of models in Magento:
- Simple
This type of model represents one row in table. Each row completely represents information about an entity.Example: Core config, Url rewrite etc. - Complex
This type of model’s information is represented in more than one table.Example: CMS Page, Sales Orders etc. - EAV ( Entity Attribute Value )
This is the most powerful and most confusing ( at-least for me ) models. It is the most flexible model as it allows us to add any number of attributes we want.Example: Products, Categories, Customers etc.
Understanding EAV : Entity, Attribute, Value
In this type of model, an entity data is divided into three parts and stored in three different tables.
The Entity table contains the key columns of the entity like customer_id, customer_email etc.
The Attribute table contains list of all attributes.
The Value table maps the attributes to their values.
Let us first check how values are stored in a normal scenario. Let us say we have a customer table with following attributes:
id, email, password, first_name, last_name, gender, created_at, updated_at, status
Now at this point, we can think about only this much number of attributes. In the long run in a big application like e-commerce, the number of fields might grow.
In Magento, we use various third party plugins that adds their own fields to existing entities. In that case, it would be very tough to manage everything in a single table.
That is why Magento developers used the EAV concept that allows application to modify the existing entities dynamically. You can add/remove any number of attributes to an entity.
EAV model adds overhead too for developers :). Since it uses table joins to fetch data from multiple tables, it is slower as well. Since, Magento uses the concept of caching, it should not hurt the website performance.
Simple model’s table
customer_id | first_name | last_name | created_at | status | |
1 | ashutosh@gmail.com | Ashutosh | Pandey | 2015-01-01 10:25 | active |
EAV model’s tables
Customer entity
This table store important attributes of the entity.
customer_id | created_at | status | |
1 | ashutosh@gmail.com | 2015-01-01 10:25 | active |
Customer attributes
This table stores attributes only.
attribute_id | attribute_name |
1 | first_name |
2 | last_name |
3 | gender |
Customer attribute values
This table stores attribute values corresponding to their entities.
value_id | entity_id | attribute_id | value |
1 | 1 | 1 | Ashutosh |
1 | 1 | 2 | Pandey |
1 | 1 | 3 | Male |
Really Awesome!
thanks 4 this really its very important and nice.