EAV( Entity-Attribute-Value): Everything you need to know about this model in Magento 2

EAV( Entity-Attribute-Value) is extremely essential for every developer to build products or do projects at a higher customization level.  In today’s post, we’re going to show you guys everything you need to know about this model in Magento 2

What is Entity Attribute Value?

Entity Attribute Value

EAV (Entity Attribute Value) is a data model used in Magento 2. It is the core system when your business directly operates with Magento 2 platform. Using the EAV system enables auto-extending your models instead of there being an intervention into the database. EAV stands for Entity, Attribute, and Value.

  • Entity: The entity stands for Magento data items such as products, categories, customers, and orders. Each entity will have its own entity record in the database.
catalog_category_entity
catalog_product_entity
customer_entity
customer_address_entity
  • Attribute: The attributes stand for data products that belong to an entity. For instant, the product entity has attributes such as name, price, status, and many more.
SELECT
  attribute_code,
  attribute_id,
  backend_type
FROM
  eav_attribute
WHERE entity_type_id =
  (SELECT
    entity_type_id
  FROM
    eav_entity_type
  WHERE entity_type_code = 'catalog_product')
  • Value: Value connects to the exact content of the characteristic of the entity. Like color has value black, size has value XL, etc. In Magento 2 values of an attribute of an entity are stored in catalog_product_entity_[backend_type] tables.Where [backend_type] connects to the value of field: backend_type (except value ‘static’) of table: eav_attribute.

E.g. To find out all the backend types related to a product entity:

SELECT DISTINCT
  backend_type
FROM
  eav_attribute
WHERE entity_type_id =
  (SELECT
    entity_type_id
  FROM
    eav_entity_type
  WHERE entity_type_code = 'catalog_product')

This shows that values are stored in the below tables for the product entity.

catalog_product_entity_text
catalog_product_entity_int
catalog_product_entity_varchar
catalog_product_entity_decimal
catalog_product_entity_datetime

Together, the EAV structure support to get flexibility for your data through flexible attributes that are added dynamically.

Advantages of Using EAV Model Structure

  • It’s quick to implement.
  • It greatly improves flexibility to website, access, and retrieves data and attributes.
  • You can freely add attributes to any entity without having to modify your core database structure.

Based on these benefits, if you’re pondering building and using an EAV module in your Magento 2 project, then you must know that the EAV also has some weaknesses.

Disadvantages of Using EAV Model Structure

  • It’s a quite complicated structure compared to the flat table structure.
  • The EAV flexibility basically slows down the SQL Queries’ execution.

Magento EAV Entity type in Magento 2 

Magento EAV Entity type in Magento 2

There are 8 types of entities in Magento 2

  • customer – Entity Id = 1
  • customer_address – Entity Id = 2
  • catalog_category – Entity Id = 3
  • catalog – Entity Id = 4
  • order – Entity Id = 5
  • invoice – Entity Id = 6
  • credit memo – Entity Id = 7
  • shipment – Entity Id = 8

(You can see it in the entity_value_type table)

Each entity type gets its own table. For example: customer – Entity consists of customer_eav_attribute, customer_eav_attribute_website, customer_entity, customer_entity_datetime, customer_entity_decimal, customer_entity_int, customer_entity_text, customer_entity_varchar (each table is corresponding to each attribute)

Structure of  Magento EAV tables and data 

Have a look at the following diagram of catalog_product eav to learn how Magento builds the EAV system:

  • catalog_product_entity: determines entity containing attribute data (with attribute type as static) — It is similar to the product table.
  • eav_attribute: is the table of attribute information.
  • catalog_product_entity_datetime, catalog_product_entity_decimal, catalog_product_entity_int, catalog_product_entity_text, catalog_product_entity_varchar: includes attribute values — It is equivalent to attribute_value table.
  • Moreover, when you add an attribute, it is put to the default attribute set and attribute group according to the information in the table (if this attribute is not declared):

How to Set Magento EAV attribute for product Magento 2

EAV attribute

1, Generate eavsetup factory

Run the following script and complete the creation EavSetup factory

/**
     * @var EavSetupFactory
     */
    protected $eavSetupFactory;
 
    /**
     * UpgradeData constructor
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    } 

2, Add EAV attribute

There are some characters that need to be followed:

  • is_featured: enter attribute code
  • group: declare the group’s name in the backend
  • type: set type of data
  • globar: define the scope of the attribute (store, website, or global)
  • visible_on_frontend: select the true or false option to show the attribute on the frontend or not
  • apply_to: assign to the exact product type that you want to set the attribute
/** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            /**
            * Add attributes to the eav/attribute
            */
            
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'is_featured',
                [
                    'group' => 'General',
                    'type' => 'int',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Is Featured',
                    'input' => 'boolean',
                    'class' => '',
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '1',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => false,
                    'unique' => false,
                    'apply_to' => ''
                ]
            );

You can also remove  the EAV attribute from the product by running the command:

$entityTypeId = 4; // Find these in the eav_entity_type table
            $eavSetup->removeAttribute($entityTypeId, 'is_featured');

Conclusion

EAV is probably the most difficult concept for new Magento developers to grasp. While the Magento EAV concept is not unique to Magento, it is rarely implemented in modern systems. We hope this will help you understand the basics of this module in Magento 2. If you still need any assistance with that, connect with our Magento Website Development service. We – MageSolution is providing the best Magento solutions for you that can skyrocket your revenue and increase conversions. Contact us for a free consultation!

Magento customer attribute: How to create it for Magento 2 stores