1. Magento EAV System
The EAV system is one of the most complex part of Magento system.
TRUONG DINH KHOA
Email: mrkhoa99@gmail.com
2. Do you know?
MySQL has a hard limit of 4096 columns per table.
Oracle has even less: 1000.
Microsoft SQL Server offers up to 30,000 columns.
3. Traditional model
In a traditional database model, tables have a fixed number of columns
products
product_id
name
price
atribute_1
……
product_id name price attribute_1 … …
000001 T-Shirt 16.9 Value 1 … …
000002 EAV Book 19 Value 2 … …
4. EAV Model
However, if entities have a huge of possible attributes? What happens? E.g. a
product has a lot of attribute: price, name, color, image, tax, manufacturer , attribute_1…
attribute_n, etc..
5. EAV Model
The problem can be addressed by EAV (row modelling).
Traditional model : adding more attributes => more columns.
EAV model: adding more attributes => more rows.
EAV stands for entity, attribute and value.
Many cloud computing platforms (Amazon, Google, Microsoft, etc.) offer EAV
Model as featuring data stores.
6. EAV Model
“E” in EAV stands for Entity. In Magento case, they are such as product,
customer, category, order, etc.
“A” stands for Attribute. These are the individual properties of each of the
entities, e.g. name, weight, email address, etc.
“V” stands for Value. The value of any particular attribute.
7. EAV Model
Magento Model Entity Model
EAV Attribute
Value Table
(varchar)
Value Table
(text)
Value Table
(int)
Value Table
(etc)
8. EAV Model
For example : Catalog Product
catalog_product_entity
(entity_type_id)
eav_attribute
(entity_type_id,
attribute_id)
catalog_product_entity_attribute_varchar
(entity_type_id, attribute_id)
catalog_product_entity_attribute_text
(entity_type_id, attribute_id)
catalog_product_entity_attribute_int
(entity_type_id, attribute_id)
catalog_product_entity_attribute_etc
(entity_type_id, attribute_id)
9. Creating simple raw sql
SELECT a list of product
3 tables:
catalog_product_entity ,
eav_attribute,
catalog_product_entity_varchar
10. Creating simple raw sql
-eav_attribute:
+attribute_id: unique identifier and primary key of table.
+entity_type_id: relates each attribute to a EAV model type.
+attribute_code: the name or key of attributes.
+….
-catalog_product_entity:
+entity_id: unique identifier of product.
+entity_type_id: identifies each type of EAV model type.
+….
11. Creating simple raw sql
-catalog_product_entity_varchar
+value_id: unique identifier and primary key
+entity_type_id: this value belongs to product entity.
+attribute_id: a foreign key- references to attribute_id on eav_attribute
+…
12. Creating simple raw sql
Raw SQL:
SELECT
p.entity_id AS product_id,
var.value AS product_name,
p.sku AS product_sku
FROM
catalog_product_entity p,
eav_attribute eav,
catalog_product_entity_varchar var
WHERE p.entity_type_id = eav.entity_type_id
AND var.entity_id = p.entity_id
AND eav.attribute_code = 'name'
AND eav.attribute_id = var.attribute_id
ORDER BY `name` ASC
13. What’s next?
Creating a raw sql shows a small piece. It is easy to understand for new
Magento developers. But for customizing and maintaining purpose, what
programming technique is used?
In a big picture, we need to do more for our software => Magento used Object
Relational Mapping (ORM) system.
14. Magneto ORM System
Object-relational mapping (ORM, O/RM, and O/R mapping) in computer
software is a programming technique for converting data between
incompatible type systems in object-oriented programming languages. This
creates, in effect, a "virtual object database" that can be used from within
the programming language.
https://en.wikipedia.org/wiki/Object-relational_mapping
15. Magento ORM System
Building a simple query to retrieve products listing:
//Instantiate a product collection:
$collection = Mage::getModel('catalog/product')->getCollection();
//Select an attribute:
$collection->addAttributeToSelect('name');
//Sort the collection:
$collection->setOrder('name', 'asc');
//Load the collection:
$collection->load();
//Print the collection
echo $collection->getSelect();
16. Magento ORM System
SELECT
`e`.*,
IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS
`name`,
`price_index`.`price`, `price_index`.`tax_class_id`,
`price_index`.`final_price`,
IF(
price_index.tier_price IS NOT NULL,
LEAST(
price_index.min_price,
price_index.tier_price
),
price_index.min_price
) AS `minimal_price`,
`price_index`.`min_price`, `price_index`.`max_price`,
`price_index`.`tier_price`
FROM
`catalog_product_entity` AS `e`
LEFT JOIN `catalog_product_entity_varchar` AS `at_name_default`
ON (`at_name_default`.`entity_id` = `e`.`entity_id`)
AND (`at_name_default`.`attribute_id` = '71')
AND `at_name_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_varchar` AS `at_name`
ON (`at_name`.`entity_id` = `e`.`entity_id`)
AND (`at_name`.`attribute_id` = '71')
AND (`at_name`.`store_id` = 1)
INNER JOIN `catalog_product_index_price` AS `price_index`
ON price_index.entity_id = e.entity_id
AND price_index.website_id = '1'
AND price_index.customer_group_id = 0
ORDER BY `name` ASC
17. Magento ORM System
Some differences in comparison with a raw sql:
-There are many columns.
-Getting products in current store: at_name_default`.`store_id` = 0
-Add more conditions: e.g. IF(at_name.value_id > 0, at_name.value,
at_name_default.value)
-etc.
18. Magento ORM System
The most useful collection methods
addAttributeToSelect: adding an attribute to entities in a collection
addAttributeToFilter: using to filter a collection of
EAV entities with the specified conditional parameters.
addFieldToFilter: using also to filter a collection, but for non-EAV models, this
function is used regularly.
addAttributeToSort: adding an attribute to sort order
etc.