How EAV Model Works in Magento2?
EAV Model stands for Entity-Attribute-Value is a very important concept of magento2 to understand how the attributes of entities are stored and works in magento2.
EAV Model is described as :-
Entity - Entity refers to the data items such as customers,orders,catalog,category etc.
Attribute - Attributes of the entities like price is an attribute of the product(entity) is referred here is an attribute.
Value - A value of the attribute as $20 is a value for price attribute is referred here is a value.
Use of EAV Model
EAV(Entity-Attribute-Value) is used to create new attributes of entities such as products, categories etc. With the use of eav models we don’t need to change the structure of the core tables of attributes thus it increases the scalability of the system.
Let’s have a look at how eav works in Magento2 -
Eav model used many several tables to store the data of the entity attributes. The top most table for the eav model is eav_attribute.
eav_attribute
This table is used to store all the attributes for all the entities. Every time a new attribute is created one row is added to this table with all the information of that eav attribute like attribute_id, enity_type_id, attribute_code and backend_type of the attribute etc.
These are some of the values that we need to understand about -
attribute_id - Every attribute is assigned to a unique attribute id, this id is stored in this table first column.
entity_type_id - We have different types of entities in magento2 like product, category, customers, orders, shipment etc. so we have unique if for all the entities. Here in this table entity_type_id is used to store the entity type of the attribute. For example we are creating an attribute of category then the id of category entity(3) will be stored here.
attribute_code - whenever we create a new attribute we have to set a unique attribute code. This attribute code is used to fetch the value of the attribute.
backend_model - model class which is used to set and fetch the value of the attribute is stored here.
backend_type - This is an important value for the eav model, eav model stores the values in different tables according to their backend type.
Now i think you have a better understanding about what data is stored in the database whenever we create a new attribute.
After the eav_attribute table we have many different tables to store different data.
eav_attribute_group
This table contains the information about the existing attribute groups and attribute sets in which these groups are assigned. Whenever we add an attribute to an attribute set, so before that we can add attributes to different groups, one attribute can be in many attribute groups and one group can have many attributes.
This table contains some important info about attribute group and set -
attribute_group_id which is a unique id for all the attributes group.
attribute_set_id - this is an id of that attribute in which the particular group is assigned.
attribute_group_name - group name of the attribute.
attribute_group_code - code of the attribute group.
eav_attribute_set
This table contains the relationship between the existing attribute sets and entity types in which these attributes sets are used. We can add many attribute groups in one attribute set.
attribute_set_id - a unique id for every attribute set.
entity_type_id - this is an id of the entity type in which this attribute set is used.
attribute_set_name - this is used to store the name of attribute sets.
eav_entity_attribute
Now this table contains all the relationships about which attribute is assigned to which attribute-group, attribute-set, entity_type_id etc.
Take a look at the tables structure -
Eav Model Table Structures
eav_entity_type
There are different types of entities in magento2. So we have an id of these entities and the responsible table for this entity is stored in this table.
Whenever we need to find an entity_table we can look in this table.
entity_type_id - Unique id of entity types.
entity_type_code - code of the entity type.
entity_table - Responsible tables to store the values of attributes values.
Now we know that attributes are stored in several tables according to the groups, sets and entities. Now what about the values of the attributes? Where are these stored?
Let’s take an example to understand this in a better way.
For example we have created a custom eav attribute of product “special-price”. This attribute is of product entity so if we want to find the table for product entity attribute values we need to find the root table for entity values for that we can take a look inside eav_entity_type table. There we have entity_type_code=”catalog product” and the table for the product entity is catalog_product_entity and the entity_type_id is 4.
Let's take an example to see how sku value is stored inside the eav model in magento2.
All the information about attributes are stored inside eav_attributes table.
This entity_type_id defines the entity of the attribute. To find the entity_type check into the eav_entity_type table.
Catalog_product_entity stores the values of static backend type. For example sku’s of all the products.
Attribute Values
Whenever we create a custom attribute we need to define the type of the attribute and that is referred as a backend type. Eav model stores all the values of the attributes in different tables according to their backend types. These are some of the tables of catalog entity -
Each type of entity has separate tables to store all the values for example the value of the name attribute is stored inside the catalog_product_entity_varchar table. If the price is in decimal then the value of price will be stored inside catalog_product_entity_decimal table. And all the values as static backend types are stored in catalog_product_entity table.
catalog_product_entity
This table contains the values of attributes that have “static” backend type. Basically, sku has a backend type as static so all the products sku is stored inside this table.
Have a look at few more entity backend types tables -
catalog_product_entity_int
catalog_product_entity_varchar
catalog_product_entity_decimal
I hope this blog helps you to understand the concept of eav models in magento2.
In the next blog i will explain about how to create a custom eav attribute and how eav model class works in magento2. Keep reading. Thanks in advance. :)
Comments
Post a Comment