Overview Of Magento2 EAV Attribute

Last week, we looked into overriding core files in Magento. Now, in this continuation of the Magento tutorial series, we explore the powerful Entity Attribute Value (EAV) model in Magento 2. This lesson covers three major parts:

  • EAV Model
  • Working with EAV Model
  • Working with Collections & EAV Collections

1. EAV Model

What is Entity Attribute Value (EAV)?

EAV is a data model designed for entities that can potentially have a large number of attributes, but only a few are actually used per entity. This is similar to a sparse matrix.

EAV = Entity + Attribute + Value

  • Entity: Items like product, category, customer, order.
  • Attribute: Properties of the entity (name, price, status, etc.).
  • Value: The actual data assigned to an attribute.

Entity Table Structure

EAV uses a "long and skinny" table format:

  • Entity – item being described
  • Attribute – key referencing attribute definition
  • Value – the actual data stored

Why EAV is Used

EAV provides high flexibility. New attributes can be added without modifying the database schema. Magento automatically handles saving the dynamic attributes.

Downsides of EAV

  • Slow queries due to multiple joins
  • Higher learning curve for developers
  • Complex structure compared to flat tables

Working with EAV Model

EAV requires converting between EAV row format and relational column format. This can be CPU-intensive and error-prone.

Querying EAV is inherently slower due to table fragmentation and joins. It is a trade-off between flexibility (schema-free structure) and performance.

EAV Storage Tables in Magento

  • eav_entity_type – stores entity types & default attribute sets
  • eav_entity – stores entities
  • eav_entity_attribute – defines attribute grouping
  • eav_entity_value tables – value tables by type:
    • eav_entity_int
    • eav_entity_varchar
    • eav_entity_text
    • eav_entity_decimal
    • eav_entity_datetime

Entity Attribute Value - Data Access Process

Reading Data

  1. Read main entity table
  2. Load available attributes
  3. Fetch values from EAV value tables
  4. Map values into the model

Saving Data

  1. Map data from model
  2. Save main entity record
  3. Save values into EAV value tables

Example Commands

php bin/magento cache:flush
php bin/magento indexer:reindex

Conclusion

Magento's EAV model offers exceptional flexibility for storing dynamic attribute-driven data. It is ideal when entities need many optional attributes, and the schema must remain extensible.

However, it comes with performance costs and complexity. Developers must understand the trade-offs and choose EAV only where necessary.