3.05

EAV Basics

Master the Entity Attribute Value framework - understanding entity types, attributes, attribute sets, and how values are stored with scope support.

Why This Matters: Understanding EAV (Entity Attribute Value) is crucial for Magento development. EAV provides the flexible attribute system that powers products, categories, and customers. This is a fundamental exam topic.

EAV Framework Overview

mindmap root((EAV Framework)) Purpose Flexible attributes Different properties Extend data architecture Components Entity Types Attribute Sets Attributes Attribute Values Entity Types catalog_product catalog_category customer customer_address Attribute Models Frontend Model Source Model Backend Model Storage entity_varchar entity_int entity_decimal entity_text entity_datetime

What is EAV?

Entity Attribute Value (EAV) is a framework that allows entities to have different values for its properties.

Two Main Purposes:

  1. Multi-valued attributes: Different values for attributes based on scope (global, website, store)
  2. Flexible data architecture: Allows extending an entity's data architecture easily
Example: EAV allows laptops and mobile phones, which are instances of the Catalog_Product entity, to have different properties (attributes). Some products have "screen_size", others have "battery_capacity", etc.

EAV Implementation

EAV is technically implemented via ResourceModel.

⚠️ Important: It is very difficult to create a custom EAV entity, and usually there is no need for that. Use extension attributes instead for custom entities.

Four Components of EAV

EAV consists of the following components:

  1. Entity Types - Registered EAV entities
  2. Attribute Sets - Groups of attributes
  3. Attributes - Individual properties with behavior
  4. Attribute Values - Stored values in type-specific tables

1. Entity Types

eav_entity_type Table

Each EAV entity must be registered as an entity type in the eav_entity_type table.

SELECT entity_type_id, entity_type_code, entity_table 
FROM eav_entity_type;

+----------------+------------------+-------------------------+
| entity_type_id | entity_type_code | entity_table            |
+----------------+------------------+-------------------------+
| 1              | customer         | customer_entity         |
| 2              | customer_address | customer_address_entity |
| 3              | catalog_category | catalog_category_entity |
| 4              | catalog_product  | catalog_product_entity  |
| 5              | order            | sales_order             |
| 6              | invoice          | sales_invoice           |
| 7              | creditmemo       | sales_creditmemo        |
| 8              | shipment         | sales_shipment          |
+----------------+------------------+-------------------------+

Entity Types Evolution

There are eight entity types in Magento. They come from the early days of Magento 1, and by Magento 2.4, have evolved significantly.

Entity Type Status Notes
catalog_product Fully-Fledged Complete EAV implementation with all features
catalog_category Fully-Fledged Complete EAV implementation with all features
customer Partial Some EAV features, but on a much smaller scale
customer_address Partial Some EAV features, but on a much smaller scale
order Rudimentary Has increment model from EAV framework, not much else
invoice Rudimentary Has increment model, pretty much nothing left from EAV
creditmemo Rudimentary Pretty much nothing left from EAV
shipment Rudimentary Pretty much nothing left from EAV

Catalog Module vs Eav Module

Important Distinction: Many EAV features are developed in the Magento_Catalog module rather than in Magento_Eav.

For example, the celebrated multi-scope functionality of EAV attributes is a core feature of the EAV framework, however it is fully implemented in the Catalog module.

⚠️ Consequence: Customer's attributes will have problems with multi-scoped values. Out of the box, all customer's attributes are global (and static).

Verify Customer Attributes:

SELECT attribute_id, attribute_code, backend_type, backend_table 
FROM eav_attribute 
WHERE entity_type_id=1;

2. Attribute Sets

Attribute sets are groups of attributes. See Topic 3.01 for comprehensive coverage of attribute sets.

Key Points:

  • Stored in eav_attribute_set table
  • Contain attribute groups
  • Allow different products to have different attributes
  • Can be created via admin or programmatically

3. Attributes

Attribute Storage

Each eav_attribute has a lot of information associated with it, besides its values. That information can be generic or entity type specific.

Information Type Storage Table Applies To
Generic information eav_attribute All attributes
Category/Product specific catalog_eav_attribute Products and categories
Customer specific customer_eav_attribute Customers and customer addresses

Creating Attributes

While it is possible to add attributes via an admin interface, we should try to do that programmatically via a Data Patch.

✅ Best Practice: Attributes added through the admin panel will only be available in a single copy of the database, while those added by a Data Patch will be reproduced every time a new database is deployed.

Further Reading:

Three Important Attribute Models

Attributes have their own properties which define an attribute's behavior. Three models are of great importance:

  1. Frontend Model - Formats/adjusts value on frontend
  2. Source Model - Provides list of acceptable options
  3. Backend Model - Controls how value is saved to database

Frontend Model

Purpose

Formats or adjusts the value of the attribute on the frontend.

Implementation:

The value of the attribute's frontend_model property must be set to a class that:

  • Implements Magento\Eav\Model\Entity\Attribute\Frontend\FrontendInterface
  • OR extends Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend (more meaningful)

Key Method:

public function getValue(\Magento\Framework\DataObject $object)
{
    $value = $object->getData($this->getAttribute()->getAttributeCode());
    // Format or adjust the value here
    return $value;
}
Main Purpose: The main purpose of the frontend model is to render an attribute on the storefront, on the product view page.

Source Model

Purpose

Provides a list of acceptable options for an attribute. The most basic example would be boolean options.

Main Purpose: Provide options for select-type attributes (select and multiselect).

Source Model Implementation

A source model must:

  • Implement \Magento\Eav\Model\Entity\Attribute\Source\SourceInterface
  • OR extend \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource

Native Implementations:

Class Purpose
Magento\Eav\Model\Entity\Attribute\Source\Boolean Provides options for boolean dropdowns (Yes/No)
Magento\Eav\Model\Entity\Attribute\Source\Table Used very often, provides option values from database
Magento\Eav\Model\Entity\Attribute\Source\Config Allows you to specify options in config

Catalog-Specific Source Model Methods

For catalog entities, a source model may implement additional methods:

  • getFlatColumns() - Used in the indexing process
  • addValueSortToCollection() - Allows custom logic for sorting by this attribute

Reference Implementation:

Magento\Catalog\Model\Product\Attribute\Source\Status

Backend Model

Purpose

Controls how the attribute's value is saved to the database.

Implementation:

Backend model must:

  • Implement \Magento\Eav\Model\Entity\Attribute\Backend\BackendInterface
  • OR extend \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend (more meaningful)

Backend Model Methods

Methods of Interest:

Method Purpose
afterLoad() React after entity is loaded
beforeSave() Modify data before saving
afterSave() Additional operations after save
validate() Implement backend-level validation for attribute saving
⚠️ Important: Usually you want to ensure that AbstractBackend::validate() is executed, since it has some valuable logic.

Example:

Magento\Eav\Model\Attribute\Backend\Data\Boolean

4. Attribute Values

Storage Tables

Values are stored in a set of tables, specific per entity type.

Example for catalog_product:

SHOW TABLES LIKE 'catalog_product_entity_%';

+-------------------------------------------------------+
| Tables_in_magento (catalog_product_entity_%)         |
+-------------------------------------------------------+
| catalog_product_entity_datetime                       |
| catalog_product_entity_decimal                        |
| catalog_product_entity_gallery                        |
| catalog_product_entity_int                            |
| catalog_product_entity_media_gallery                  |
| catalog_product_entity_media_gallery_value            |
| catalog_product_entity_media_gallery_value_to_entity  |
| catalog_product_entity_media_gallery_value_video      |
| catalog_product_entity_text                           |
| catalog_product_entity_tier_price                     |
| catalog_product_entity_varchar                        |
+-------------------------------------------------------+

Value Table Structure

Each value table stores:

  • entity_id - The entity (product, category, etc.) ID
  • attribute_id - The attribute ID
  • value - The actual value
  • store_id - Indicator for scope
Important: The store_id's interpretation depends on the attribute's scope: global, website, or store.

Understanding Scope with store_id

Example Scenario:

Say there is a record with:

  • attribute_id = 25
  • store_id = 4
  • value = 133

Step-by-Step Interpretation:

  1. Check the scope of attribute with id 25 in eav_attribute, is_global field
  2. Assume is_global = 2, which means website scope
  3. So the attribute has different values on different websites
  4. Its value for website with id 4 equals 133
is_global Value Scope Type store_id Interpretation
0 Store View Refers to store_id in store table
1 Global Always 0 (applies to all stores)
2 Website Refers to website_id in store_website table

EAV Value Storage by Type

varchar Tables

Storage: *_entity_varchar

Data Type: Short text (up to 255 characters)

Example: SKU, name, short description

text Tables

Storage: *_entity_text

Data Type: Long text

Example: Description, custom options

int Tables

Storage: *_entity_int

Data Type: Integer values

Example: Status, visibility, category IDs

decimal Tables

Storage: *_entity_decimal

Data Type: Decimal/float values

Example: Price, weight, special price

datetime Tables

Storage: *_entity_datetime

Data Type: Date and time values

Example: Special price from/to dates, news from date

Exam Tips

Key Points to Remember

  • EAV Purpose: Flexible attributes, different properties per entity, extend data architecture
  • Four Components: Entity types, attribute sets, attributes, attribute values
  • Entity Type Table: eav_entity_type
  • Fully-Fledged EAV: Only catalog_product and catalog_category
  • Customer Attributes: Partial EAV support, mostly global scope
  • Multi-scope: Implemented in Magento_Catalog, not Magento_Eav
  • Attribute Info Tables: eav_attribute (generic), catalog_eav_attribute (catalog), customer_eav_attribute (customer)
  • Create Attributes: Use Data Patches, not admin (for deployment)
  • Frontend Model: Formats value on frontend, implements FrontendInterface, getValue() method
  • Source Model: Provides options for select/multiselect, implements SourceInterface
  • Backend Model: Controls save to database, implements BackendInterface, afterLoad/beforeSave/afterSave/validate methods
  • Common Source Models: Boolean, Table, Config
  • Value Tables: *_entity_varchar, *_entity_int, *_entity_decimal, *_entity_text, *_entity_datetime
  • Value Table Columns: entity_id, attribute_id, value, store_id
  • store_id Interpretation: Depends on is_global (0=store, 1=global, 2=website)
  • Scope Values: 0=Store View, 1=Global, 2=Website
  • Custom EAV Entity: Very difficult, use extension attributes instead

Further Reading