EAV Basics
Master the Entity Attribute Value framework - understanding entity types, attributes, attribute sets, and how values are stored with scope support.
EAV Framework Overview
What is EAV?
Entity Attribute Value (EAV) is a framework that allows entities to have different values for its properties.
Two Main Purposes:
- Multi-valued attributes: Different values for attributes based on scope (global, website, store)
- Flexible data architecture: Allows extending an entity's data architecture easily
EAV Implementation
EAV is technically implemented via ResourceModel.
Four Components of EAV
EAV consists of the following components:
- Entity Types - Registered EAV entities
- Attribute Sets - Groups of attributes
- Attributes - Individual properties with behavior
- 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
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.
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_settable - 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.
Further Reading:
- How to Add a New Product Attribute
- Adding Customer EAV Attribute for Backend Only
Three Important Attribute Models
Attributes have their own properties which define an attribute's behavior. Three models are of great importance:
- Frontend Model - Formats/adjusts value on frontend
- Source Model - Provides list of acceptable options
- 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;
}
Source Model
Purpose
Provides a list of acceptable options for an attribute. The most basic example would be boolean options.
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 processaddValueSortToCollection()- Allows custom logic for sorting by this attribute
Reference Implementation:
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 |
AbstractBackend::validate() is executed, since it has some valuable logic.
Example:
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.) IDattribute_id- The attribute IDvalue- The actual valuestore_id- Indicator for scope
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 = 25store_id = 4value = 133
Step-by-Step Interpretation:
- Check the scope of attribute with id 25 in
eav_attribute,is_globalfield - Assume
is_global = 2, which means website scope - So the attribute has different values on different websites
- 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