Magento 2 EAV & Attributes
Before writing code
Fetch live docs:
- Web-search
site:developer.adobe.com commerce php development components attributes for attribute development guide
- Web-search
site:developer.adobe.com commerce php tutorials backend custom-attributes for attribute creation tutorial
- Web-search
magento 2 EAV attribute data patch for current patterns using data patches
Conceptual Architecture
What EAV Is
Entity-Attribute-Value is Magento's flexible storage pattern. Instead of one column per attribute, values are stored in separate typed tables. This allows merchants to add unlimited attributes without schema changes.
EAV Entities
Four entity types use EAV:
- Products (
catalog_product_entity)
- Categories (
catalog_category_entity)
- Customers (
customer_entity)
- Customer Addresses (
customer_address_entity)
EAV Table Structure
Each entity has typed value tables:
*_entity — base entity table (entity_id, sku, type_id, etc.)
*_entity_varchar — short text values
*_entity_int — integer values (including boolean, select)
*_entity_decimal — decimal/price values
*_entity_datetime — date/time values
*_entity_text — long text values
Attribute Properties
Key properties when creating attributes:
- type — backend storage type (varchar, int, decimal, datetime, text, static)
- input — frontend input type (text, textarea, select, multiselect, boolean, date, price, media_image, etc.)
- label — human-readable name
- required — is this attribute mandatory?
- source — source model for select/multiselect options
- backend — backend model for validation/processing
- frontend — frontend model for display formatting
- global — scope: store view, website, or global
- visible — shown in admin forms
- searchable — indexed for catalog search
- filterable — available as layered navigation filter
- comparable — shown in product comparison
- used_in_product_listing — available in category listing pages
Attribute Sets and Groups
- Attribute Set — a named collection of attributes (e.g., "Default", "Clothing", "Electronics")
- Attribute Group — organizes attributes within a set into tabs (e.g., "General", "Prices", "Images")
- Every product is assigned one attribute set
- The Default attribute set contains core attributes
Creating Attributes via Data Patch
The modern approach uses Data Patches with EavSetupFactory:
- Create a data patch class in
Setup/Patch/Data/
- Inject
EavSetupFactory (or CategorySetupFactory for categories)
- Call
$eavSetup->addAttribute() with entity type, code, and properties
- Assign to attribute set/group
Source Models
For select/multiselect attributes, source models provide options:
- Built-in:
Magento\Eav\Model\Entity\Attribute\Source\Boolean, Table
- Custom: extend
AbstractSource, implement getAllOptions()
Static Attributes
Attributes with type static are stored as columns directly on the entity table rather than in EAV value tables. Used for frequently queried fields (e.g., sku, type_id, created_at).
Best Practices
- Use data patches (not install/upgrade scripts) for attribute creation
- Choose the correct backend type —
int for select/boolean, decimal for prices
- Set
searchable, filterable, comparable thoughtfully — each adds indexing overhead
- Use
static type sparingly — it modifies the entity table schema
- Test attribute creation on a clean install and with
setup:upgrade
Fetch the attribute development docs for exact addAttribute() parameters, source model patterns, and current entity type constants before implementing.
1---2name: magento-eav-attributes3description: Work with Magento 2 EAV (Entity-Attribute-Value) system — create custom attributes, attribute sets, manage EAV tables, and understand the EAV data model. Use when adding product/category/customer attributes or working with the attribute system.4---5
6# Magento 2 EAV & Attributes
7
8## Before writing code
9
10**Fetch live docs**:
111. Web-search `site:developer.adobe.com commerce php development components attributes` for attribute development guide
122. Web-search `site:developer.adobe.com commerce php tutorials backend custom-attributes` for attribute creation tutorial
133. Web-search `magento 2 EAV attribute data patch` for current patterns using data patches
14
15## Conceptual Architecture
16
17### What EAV Is
18
19Entity-Attribute-Value is Magento's flexible storage pattern. Instead of one column per attribute, values are stored in separate typed tables. This allows merchants to add unlimited attributes without schema changes.
20
21### EAV Entities
22
23Four entity types use EAV:
24- **Products** (`catalog_product_entity`)
25- **Categories** (`catalog_category_entity`)
26- **Customers** (`customer_entity`)
27- **Customer Addresses** (`customer_address_entity`)
28
29### EAV Table Structure
30
31Each entity has typed value tables:
32- `*_entity` — base entity table (entity_id, sku, type_id, etc.)
33- `*_entity_varchar` — short text values
34- `*_entity_int` — integer values (including boolean, select)
35- `*_entity_decimal` — decimal/price values
36- `*_entity_datetime` — date/time values
37- `*_entity_text` — long text values
38
39### Attribute Properties
40
41Key properties when creating attributes:
42- **type** — backend storage type (varchar, int, decimal, datetime, text, static)
43- **input** — frontend input type (text, textarea, select, multiselect, boolean, date, price, media_image, etc.)
44- **label** — human-readable name
45- **required** — is this attribute mandatory?
46- **source** — source model for select/multiselect options
47- **backend** — backend model for validation/processing
48- **frontend** — frontend model for display formatting
49- **global** — scope: store view, website, or global
50- **visible** — shown in admin forms
51- **searchable** — indexed for catalog search
52- **filterable** — available as layered navigation filter
53- **comparable** — shown in product comparison
54- **used_in_product_listing** — available in category listing pages
55
56### Attribute Sets and Groups
57
58- **Attribute Set** — a named collection of attributes (e.g., "Default", "Clothing", "Electronics")
59- **Attribute Group** — organizes attributes within a set into tabs (e.g., "General", "Prices", "Images")
60- Every product is assigned one attribute set
61- The Default attribute set contains core attributes
62
63### Creating Attributes via Data Patch
64
65The modern approach uses Data Patches with `EavSetupFactory`:
661. Create a data patch class in `Setup/Patch/Data/`
672. Inject `EavSetupFactory` (or `CategorySetupFactory` for categories)
683. Call `$eavSetup->addAttribute()` with entity type, code, and properties
694. Assign to attribute set/group
70
71### Source Models
72
73For select/multiselect attributes, source models provide options:
74- Built-in: `Magento\Eav\Model\Entity\Attribute\Source\Boolean`, `Table`
75- Custom: extend `AbstractSource`, implement `getAllOptions()`
76
77### Static Attributes
78
79Attributes with type `static` are stored as columns directly on the entity table rather than in EAV value tables. Used for frequently queried fields (e.g., `sku`, `type_id`, `created_at`).
80
81### Best Practices
82
83- Use data patches (not install/upgrade scripts) for attribute creation
84- Choose the correct backend type — `int` for select/boolean, `decimal` for prices
85- Set `searchable`, `filterable`, `comparable` thoughtfully — each adds indexing overhead
86- Use `static` type sparingly — it modifies the entity table schema
87- Test attribute creation on a clean install and with `setup:upgrade`
88
89Fetch the attribute development docs for exact `addAttribute()` parameters, source model patterns, and current entity type constants before implementing.