📚 Introduction to Magento 2 EAV Model
Magento 2 uses two main data models: Flat Tables and EAV (Entity-Attribute-Value). The EAV model is used for flexible, dynamic data—like products and customers—where attributes can vary.
If you’ve wondered how Magento stores different product types with hundreds of attributes—EAV is the answer!
🧠 What is EAV?
In simple words, the EAV model breaks down the structure into 3 parts:
- Entity: The main object (like a product)
- Attribute: A property (like color, size, weight)
- Value: The data (e.g., red, XL, 1.5kg)
🏗️ Real Example: How Product Data Is Stored
Let’s say you have a T-shirt with these details:
- Name: “Cool T-Shirt”
- Color: “Red”
- Size: “Large”
In an EAV model, this will be stored across multiple tables:
🔍 Sample Tables in Magento
catalog_product_entity
– Stores base info like entity_ideav_attribute
– Defines all possible attributes (name, color, size)catalog_product_entity_varchar
– Stores string values like name and colorcatalog_product_entity_int
– Stores integer values (e.g., yes/no fields)catalog_product_entity_decimal
– Stores decimal values like weight
💡 Let’s Look at EAV in Action (Simplified)
🗂️ catalog_product_entity
+-----------+--------------+ | entity_id | sku | +-----------+--------------+ | 1 | cool-shirt | +-----------+--------------+
🧾 eav_attribute
+----------------+-------------+---------------+ | attribute_id | attribute_code | backend_type | +----------------+-------------+---------------+ | 73 | name | varchar | | 81 | color | varchar | | 82 | size | varchar | +----------------+-------------+---------------+
📦 catalog_product_entity_varchar
+-----------+--------------+-------------+ | value_id | attribute_id | value | +-----------+--------------+-------------+ | 1001 | 73 | Cool T-Shirt| | 1002 | 81 | Red | | 1003 | 82 | Large | +-----------+--------------+-------------+
📐 Why Magento Uses EAV
The EAV model allows Magento to:
- Add new attributes without altering the database schema
- Support multiple languages and stores
- Allow merchants to customize product data easily
⚖️ EAV vs Flat Table – Quick Comparison
Feature | EAV | Flat Table |
---|---|---|
Flexibility | High | Low |
Performance | Slower | Faster |
Schema Changes | Not Needed | Required |
🛠️ Example: Add a Custom Attribute to Product
🔧 app/code/Vendor/Module/Setup/Patch/Data/AddCustomProductAttribute.php
<?php namespace Vendor\Module\Setup\Patch\Data; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; class AddCustomProductAttribute implements DataPatchInterface { private $moduleDataSetup; private $eavSetupFactory; public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->eavSetupFactory = $eavSetupFactory; } public function apply() { $this->moduleDataSetup->getConnection()->startSetup(); $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'custom_text_field', [ 'type' => 'varchar', 'label' => 'Custom Text Field', 'input' => 'text', 'required' => false, 'sort_order' => 100, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'visible' => true, 'user_defined' => true, 'group' => 'General', ] ); $this->moduleDataSetup->getConnection()->endSetup(); } public static function getDependencies() { return []; } public function getAliases() { return []; } }
🚀 Final Step: Run Patch
bin/magento setup:upgrade
✅ Conclusion
Magento 2’s EAV model is powerful and flexible, perfect for complex, dynamic product structures. You now understand how entities, attributes, and values link together and how to add your own attributes using a setup patch.
In the next tutorial, we’ll show you how to create a custom EAV entity from scratch. Stay tuned!