Magento EAV Model - Learn Everything about Entity Attribute Value

Magento EAV Model - Learn Everything about Entity Attribute Value

The Magento EAV model manages multiple entities with various attributes. As an entity, attribute & value model, it is a highly flexible data model that helps you efficiently manage your Magento store's product data. This tutorial simplifies this important feature, helping you effectively utilize it for a more dynamic use of your online store's database.

Key Takeaways

  • Understand the basics of Magento's Entity-Attribute-Value (EAV) model.

  • Get insights into how EAV organizes data with entities, attributes, and values, enhancing your store's database management.

  • Discover the benefits of using the EAV model, like adding new attributes easily and efficiently managing database resources.

  • Acknowledge the complexities and performance issues that can arise with the EAV model and how to mitigate them.

  • Compare the traditional flat model with EAV, highlighting the flexibility and performance aspects of each.

  • Follow practical steps to create new EAV attributes in Magento, enhancing your product data management.

  • Understand how Magento hosting affects the performance of the EAV model, ensuring smooth operations for your store.

  • Find answers to common questions about the Magento EAV model.

What is the Magento EAV Model?

The Magento EAV model stands for "entity, attribute, and value." It lets you easily add entities and attributes about them, and each attribute pair is saved as one row in the EAV table.


The Entity Attribute Value (EAV) model is a data model used to describe entities. It allows for many attributes (properties and parameters) that can be used to describe these entities. However, in practice, only a relatively modest number of attributes will apply to a given entity. This model efficiently manages and organizes data, ensuring flexibility and scalability while keeping the description concise and relevant. Some call it the open schema or object attribute value model, too.


Each object in the EAV model has common facts, like its name and what it does. All these details are kept safe in an Objects table. So, the Magento EAV model is a smart way to store all the details of your online store items without stress.

Understanding EAV (Entity, Attribute & Value)

Explained EAV Model in Magento showcasing Entity, Attribute, and Value components

  • Entity: In Magento, an entity represents various data items such as products, categories, customers, and orders. Each entity (e.g., products and Magento categories) has its database record.

  • Attribute: Attributes are data elements associated with an entity. For instance, the product entity possesses attributes like name, price, status, etc.

  • Value: The concept of value is straightforward as it represents a value associated with a specific attribute.

Importance of the Magento EAV Model

The Magento EAV model is a key tool as it helps easily manage data. This model lets you add new facts to your store without any trouble. Each fact becomes part of the whole item's story. The more facts you have, the better picture you give your customer. This model also makes wise use of space. Not knowing how it works can lead to problems with your online store's setup or update process.


It is very important for those who build or run Magento stores to know about this model because it gives shape and structure to all their site’s data. With this knowledge, they'll be able to make sure all parts are working well together and keep things running smoothly.

Entity Table Structure

Detailed structure of Magento Entity Table highlighting key components

In an entity-attribute-value (EAV) data model, every attribute-value combination represents a fact that describes an entity. In an EAV table, each row stores a single fact.


EAV tables are often called long (rows) and skinny (columns). Data is logged using three separate columns.

  • The entity: The object in question.

  • The attribute or parameter: A foreign key references a table of attribute definitions. The attribute definitions table should include columns like attribute ID, attribute name, description, and data type. Additionally, you can add extra columns for input validation. These could include maximum string length, regular expression, and permissible values.

  • The attribute's value: Each database row should have a single value for each attribute.

How Entity Attribute Value Data Storage Works in Magento?

Storing EAV (Entity-Attribute-Value) data in Magento can be quite complex, as it involves separating data for each store. Magento utilizes specific tables for data storage:

  • EAV entity type: The entity type is stored along with the model information for the entity or the default attribute set.

Overview of Magento EAV Entity Typesrepresentation

  • EAV entity: A table called eav_entity stores objects belonging to a specific entity type.

Magento EAV Entity Database Structure showcasing individual entities

  • EAV entity attribute: Attributes are categorized into groups, where a group can consist of multiple attributes, and an attribute can belong to multiple groups. An attribute set represents the count of groups. An object is associated with an attribute set.

Magento EAV Entity Attributes layout and organization

  • EAV entity value: Magento utilizes value tables corresponding to different data types to enhance data storage efficiency. For example eav_entity_datetime, eav_entity_varchar, and eav_entity_int.

Magento EAV Entity Value tables and their interconnections

How to Create Magento EAV Attributes?

Let's use an example to guide you in creating EAV attributes for the catalog entity type. You can follow a similar approach for other entity types as well.

1. Create via Setup Script

To create a Magento EAV attribute, you have two options: using the InstallData or UpgradeData setup files. Here's a code snippet demonstrating how to create the attribute via InstallData by adding a file: Vendor_Module\Setup\InstallData.php

namespace Vendor\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
/**
 * Class InstallData
 * @package Vendor\Module\Setup
 */
class InstallData implements InstallDataInterface
{
    /**
     * @var \Magento\Eav\Setup\EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * InstallData constructor.
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
     */
    public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        $eavSetup = $this->eavSetupFactory->create();
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'simple_test',
            [
                'type' => 'text',
                'group' => 'Product Details',
                'label' => 'Bss Simple Test',
                'input' => 'text',
                'backend' => '',
                'frontend' => '',
                'required' => false,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'is_used_in_grid' => false,
                'is_visible_in_grid' => false,
                'is_filterable_in_grid' => false,
                'visible' => false,
                'user_defined' => false,
                'searchable' => true,
                'filterable' => true,
                'comparable' => true,
                'filterable_in_search' => true,
                'is_html_allowed_on_front' => false,
                'used_for_promo_rules' => true,
                'visible_on_front' => false,
                'used_for_sort_by' => true,
                'unique' => false,
                'default' => '',
                'used_in_product_listing' => true,
     'source' => '',
     'option' => '',
                'sort_order' => 10,
                'apply_to' => 'simple,configurable,virtual,bundle,downloadable,grouped'
            ]
        );
        $setup->endSetup();
    }
}

2. Create via Declarative Schema

In Magento 2.3 and newer versions, it is possible to generate attributes using DataPatch. As an illustration, you can create a file for this purpose:

<?php
namespace Vendor\Module\Setup\Patch\Data;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
/**
 * Class ApplyNewAttribute
 * @package Vendor\Module\Setup\Patch\Data
 */
class ApplyNewAttribute implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * ApplyNewAttribute constructor.
     *
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function apply()
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            /**
Add your attribute here.
*/
..........
       );
        $setup->endSetup();
    }
}

How the Magento EAV Model Works?

The Magento EAV model works by storing attribute values flexibly and efficiently. In this model, the attribute values are stored in a specific data storage. The EAV table structure is used to organize and manage these attribute values.


When working with Magento collections, developers can easily retrieve and manipulate the attribute values using SQL queries. The EAV model allows adding new attributes without changing the database structure, providing flexibility for customizing and extending functionality in Magento.


This makes it easier for store owners and developers to manage and modify attributes as needed. Overall, the Magento EAV model provides a scalable solution for handling entity attributes in an organized manner.

Overview of the EAV Table Structure

The EAV table structure in Magento is designed for efficient data management. It consists of several tables storing data about entities, attributes, and values. Here's a basic overview of the main components of the EAV table structure in Magento:

Table Name Description
eav_entity_type This table keeps track of all entity types in the system, like ‘catalog_product’ or ‘customer_entity’.
eav_attribute This table contains information about all the attributes for all entities, including attribute code, backend type, and source model, among others.
eav_attribute_set This table groups several attributes under a single name, which is known as an attribute set.
eav_attribute_group This table further categorizes attributes within an attribute set by organizing them into attribute groups.
eav_entity This table is responsible for storing entity data. For instance, in the case of the ‘catalog_product’ entity type, the entity would be individual products.
eav_entity_* (e.g., eav_entity_datetime, eav_entity_decimal, eav_entity_int, eav_entity_text, eav_entity_varchar) These tables store the actual attribute values for the entities, divided by value type.

Each table in the Magento EAV model plays a specific role, and together, they provide a flexible and efficient way to manage data in Magento. Understanding this structure is essential for Magento developers when customizing or extending the functionality of Magento stores.

Benefits of the Magento EAV Model

The Magento EAV Model offers several benefits for store owners and developers. One of the key advantages is its flexibility in adding new attributes. The EAV Model allows you to easily add new product or entity attributes without changing the database structure.


This allows for greater customization and scalability as your business grows. Another benefit is the efficient use of database resources. The EAV Model optimizes storage by only storing used attribute values, which saves space compared to traditional flat models.


Managing and modifying attributes is also easier with the EAV Model. You can easily update or delete attributes without affecting other parts of your database. This gives you more control over how you manage your data. Lastly, the EAV model allows for attribute scoping. You can set different attribute values for different stores or store views, allowing for localized content and a better user experience.

Disadvantages of Using the EAV Model

  • The EAV model in Magento can be complex and difficult to work with and understand.

  • Using the EAV model in Magento can lead to slower query performance due to the need for multiple table joins.

  • The EAV model in Magento allows for dynamic attribute creation without strict data validation, leading to data inconsistencies and errors.

  • Using the EAV model can increase database size and storage requirements.

  • EAV models can be challenging to index effectively, making it harder for databases to optimize query Magento performance.

  • The EAV model in Magento can make complex queries and data analysis more difficult because data is spread across multiple tables.

Comparison between Flat Model and EAV Model

Magento's EAV Model is highly dynamic and offers a flexible data structure, whereas the Flat Model is renowned for its performance speed. The table below compares these two models based on various parameters.

Parameter Flat Model EAV Model
Data Structure Uses a single table for data storage Uses multiple tables to store data in a normalized form
Attribute Addition Requires modification of the database schema to add new attributes Allows dynamic addition of attributes without altering the database schema
Performance Provides better performance when loading collections of data due to less complex structure Lags in performance due to multiple joins required when loading collections
Data Management May present challenges in managing and modifying attributes due to rigid structure Allows easy management and modification of attributes

Despite its performance drawback, the EAV Model is often chosen in Magento due to its flexibility and more structured data storage.

Comparison between the EAV Model and the Relational Model

Comparing the Magento EAV and relational models provides a better understanding of their unique advantages and applicable scenarios. Here's a systematic breakdown of their key differences:

EAV Model Relational Model
Structure The EAV model stores each attribute-value pair as a fact describing an entity. The relational model uses a flat table structure and is the traditional keyed relational database.
Flexibility The EAV model offers flexibility in adapting to changing project requirements. The relational model may not be as flexible when the project requirements change.
Efficiency The EAV model may be more suited for scenarios with many attributes with varying values. The relational model may be more efficient for scenarios with a small number of attributes with fixed values.
Data Retrieval Data retrieval can sometimes be complex and slow due to the multiple joins. Data retrieval is straightforward and faster due to the flat table structure.

This comparison, therefore, is a guide to choosing the right model based on specific project needs and requirements.

FAQs

1. What is Magento 2 EAV?

Magento 2 EAV, or Entity Attribute Value, is a database model that stores product data in the Magento platform.


2. How does the EAV structure work with Magento?

In Magento's normalized database structure, EAV, each attribute, such as name or SKU, holds some data type and becomes a column in the table. It also stores the value of the attribute for each store view.


3. Can I add custom attributes to my Magento product page?

Yes, developers can add custom attributes like extensions to your product page using Magento data config settings. These would apply to a given entity on a per-store view basis.


4. Where are these added attributes stored in the system?

When an attribute is added, whether it’s user_defined or default value set by you, it is stored in tables created for each attribute, which forms part of the EAV database structure.


5. What are two types of attributes at play here?

The main types considered here include 'static', where all info is stored within the same table, and 'EAV', where potentially vast amounts of unique data get their dedicated storage spot while keeping core entity records relatively modest size-wise


6. Is there any problem with Magento EAV?

While the flexibility and depth offered by this model offer many benefits, it could lead to complex SQL queries involving multiple joined tables, which may slow down overall system performance.


7. How does Magento hosting affect the performance of the EAV model?

Magento hosting plays a crucial role in the efficiency of the EAV model. A robust hosting solution can handle complex SQL queries and multiple table joins inherent in the EAV structure, ensuring smooth and fast data retrieval.

Summary

Understanding the Magento EAV Model is essential for store owners and developers. It provides flexibility in managing product attributes and efficiently uses database resources. With its ability to easily add and modify attributes, the EAV model empowers users to customize their online stores effectively. Choosing a reliable Magento auto-scaling hosting provider is critical for optimized performance and seamless online store operations.

Shivendra T.
Shivendra T.
Technical Writer

Shivendra has over ten years of experience creating compelling content on Magento-related topics. With a focus on the Magento community, he shares valuable tips and up-to-date trends that provide actionable insights.


Get the fastest Magento Hosting! Get Started