Skip to main content

How EAV Model Works in Magento2?

                        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. 

  1. All the information about attributes are stored inside eav_attributes table. 



attribute_id

entity_type_id

attribute_code

backend_model

backend_type

frontend_input

74

4

sku

Magento\Catalog\Model\Product\Attribute\Backend\Sku

Static

SKU


  1. This entity_type_id defines the entity of the attribute. To find the entity_type check into the eav_entity_type table. 



entity_type_id

entity_type_code

entity_model

attribute_model

entity_table

4

catalog_product

Magento\Catalog\Model\ResourceModel\Product

Magento\Catalog\Model\ResourceModel\Eav\Attribute

catalog_product_entity


  1. Catalog_product_entity stores the values of static backend type. For example sku’s of all the products. 



entity_id

attribute_set_id

type_id

Sku

1

4

Simple

canon EOS R6 store2




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

Popular posts from this blog

Magento2 CLI

Magento2 CLI  Back after a long break! Starting again with this blog because I found it the most important to understand if we are doing the magento development as magento commands play the most important role here. It really helps us a lot if we know that lĺwhich command we need to run on this action or after any changes.  Let’s start with the basic commands that we use a lot of times during our development.  cache:clean or cache:flush php bin/magento cache:clean Or  php bin/magento  cache:flush Both of these commands are used to clean the cache of Magento2 so that your new changes can be reflected to your store.  Using cache:clean it deletes all the enabled cache of magento2. Disabled caches are not affected with cache:clean but if you are doing cache:flush then it purges all the cache storage of magento wherever its enabled or disabled or it even deletes all the third party cache along with the magento cache.  If you are doing any changes in admin configuration, layout xml file, u

Plugins in Magento2

Plugins in Magento2 Unlike Magento1, Magento2 introduces a whole new concept of Plugins or Interceptor. A plugin or interceptor is used to change the behaviour of the method of any class without affecting the actual class. This is a very powerful way to customize the core classes of Magento2.  Plugins are one of the customization methods in Magento2. In plugins, there is a design pattern called “Interception” which means “Inserting code dynamically without changing original class behaviour” and this is what plugins exactly do in Magento2. Plugins modify the behaviour of a plugin class function by intercepting a function call and adding code before, after and around that function’s call.  Plugins are used with public methods only.  Declaring Plugins To declare plugins in Magento2, we have to create di.xml according to the specific area.  Create di.xml inside Vendor/Module/etc for global.   < config >     < type name = "{ObservedType}" >       < plugin name