SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Magento EAV System
The EAV system is one of the most complex part of Magento system.
TRUONG DINH KHOA
Email: mrkhoa99@gmail.com
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.
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 … …
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..
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.
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.
EAV Model
 Magento Model Entity Model
EAV Attribute
Value Table
(varchar)
Value Table
(text)
Value Table
(int)
Value Table
(etc)
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)
Creating simple raw sql
 SELECT a list of product
 3 tables:
catalog_product_entity ,
eav_attribute,
catalog_product_entity_varchar
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.
+….
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
+…
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
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.
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
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();
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
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.
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.
EAV-Advantages
 Flexibility.
 Don’t need to change database schema when adding new attributes.
 Quick to implement.
EAV-Disadvantages
 Performance problem: a lot of tables are required when making a query with
EAV Model.
 Find more…
Thanks you!
Khoa Truong Dinh
mrkhoa99@gmail.com
 http://www.boolfly.com/
 https://github.com/mrkhoa99
 https://vn.linkedin.com/in/truongdinhkhoa
 http://stackoverflow.com/users/5179786/khoa-truongdinh

Weitere ähnliche Inhalte

Was ist angesagt?

Android ui dialog
Android ui dialogAndroid ui dialog
Android ui dialog
Krazy Koder
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 
Android intents, notification and broadcast recievers
Android intents, notification and broadcast recieversAndroid intents, notification and broadcast recievers
Android intents, notification and broadcast recievers
Utkarsh Mankad
 
Pertemuan 9 preferences dan menu
Pertemuan 9 preferences dan menuPertemuan 9 preferences dan menu
Pertemuan 9 preferences dan menu
heriakj
 

Was ist angesagt? (20)

Pemrograman Mobile Unit 2 : Dasar-dasar Flutter
Pemrograman Mobile Unit 2 : Dasar-dasar FlutterPemrograman Mobile Unit 2 : Dasar-dasar Flutter
Pemrograman Mobile Unit 2 : Dasar-dasar Flutter
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
 
Implementation of EAV pattern for ActiveRecord models
Implementation of EAV pattern for ActiveRecord modelsImplementation of EAV pattern for ActiveRecord models
Implementation of EAV pattern for ActiveRecord models
 
Android ui dialog
Android ui dialogAndroid ui dialog
Android ui dialog
 
Servlets
ServletsServlets
Servlets
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Spring & hibernate
Spring & hibernateSpring & hibernate
Spring & hibernate
 
Android intents, notification and broadcast recievers
Android intents, notification and broadcast recieversAndroid intents, notification and broadcast recievers
Android intents, notification and broadcast recievers
 
Hacking Laravel - Custom Relationships with Eloquent
Hacking Laravel - Custom Relationships with EloquentHacking Laravel - Custom Relationships with Eloquent
Hacking Laravel - Custom Relationships with Eloquent
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Chapter2 j2ee
Chapter2 j2eeChapter2 j2ee
Chapter2 j2ee
 
Enterprise java unit-1_chapter-2
Enterprise java unit-1_chapter-2Enterprise java unit-1_chapter-2
Enterprise java unit-1_chapter-2
 
Pertemuan 9 preferences dan menu
Pertemuan 9 preferences dan menuPertemuan 9 preferences dan menu
Pertemuan 9 preferences dan menu
 
Google Sheets in Python with gspread
Google Sheets in Python with gspreadGoogle Sheets in Python with gspread
Google Sheets in Python with gspread
 
JDBC
JDBCJDBC
JDBC
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Swing
SwingSwing
Swing
 

Andere mochten auch

Entity Attribute Value (Eav)
Entity   Attribute   Value (Eav)Entity   Attribute   Value (Eav)
Entity Attribute Value (Eav)
Tâm
 
Magento's Imagine eCommerce Conference 2011 - Mash-up of Magento and Salesfor...
Magento's Imagine eCommerce Conference 2011 - Mash-up of Magento and Salesfor...Magento's Imagine eCommerce Conference 2011 - Mash-up of Magento and Salesfor...
Magento's Imagine eCommerce Conference 2011 - Mash-up of Magento and Salesfor...
MagentoImagine
 

Andere mochten auch (14)

Entity Attribute Value (Eav)
Entity   Attribute   Value (Eav)Entity   Attribute   Value (Eav)
Entity Attribute Value (Eav)
 
Magento's Imagine eCommerce Conference 2011 - Mash-up of Magento and Salesfor...
Magento's Imagine eCommerce Conference 2011 - Mash-up of Magento and Salesfor...Magento's Imagine eCommerce Conference 2011 - Mash-up of Magento and Salesfor...
Magento's Imagine eCommerce Conference 2011 - Mash-up of Magento and Salesfor...
 
Extensible Data Modeling
Extensible Data ModelingExtensible Data Modeling
Extensible Data Modeling
 
Integrating Magento into Joomla!
Integrating Magento into Joomla!Integrating Magento into Joomla!
Integrating Magento into Joomla!
 
Бизнес переходит в облака
Бизнес переходит в облакаБизнес переходит в облака
Бизнес переходит в облака
 
i95Dev Magento RMS Connect | eCommerce RMS Integration | Magento eCommerce In...
i95Dev Magento RMS Connect | eCommerce RMS Integration | Magento eCommerce In...i95Dev Magento RMS Connect | eCommerce RMS Integration | Magento eCommerce In...
i95Dev Magento RMS Connect | eCommerce RMS Integration | Magento eCommerce In...
 
TYPO3 + Magento + SugarCRM + Alfresco: An Enterprise Integrated Platform
TYPO3 + Magento + SugarCRM + Alfresco: An Enterprise Integrated PlatformTYPO3 + Magento + SugarCRM + Alfresco: An Enterprise Integrated Platform
TYPO3 + Magento + SugarCRM + Alfresco: An Enterprise Integrated Platform
 
Yoav Kutner Dutchento
Yoav Kutner DutchentoYoav Kutner Dutchento
Yoav Kutner Dutchento
 
Salesforce Admin
Salesforce AdminSalesforce Admin
Salesforce Admin
 
Anypoint Salesforce Connector With Mulesoft
Anypoint Salesforce Connector With MulesoftAnypoint Salesforce Connector With Mulesoft
Anypoint Salesforce Connector With Mulesoft
 
Sql Antipatterns Strike Back
Sql Antipatterns Strike BackSql Antipatterns Strike Back
Sql Antipatterns Strike Back
 
Salesforce Data Structures
Salesforce Data StructuresSalesforce Data Structures
Salesforce Data Structures
 
Data model in salesforce
Data model in salesforceData model in salesforce
Data model in salesforce
 
Data Modeling PPT
Data Modeling PPTData Modeling PPT
Data Modeling PPT
 

Ähnlich wie EAV Sytem- Magento EAV Model

Gail villanueva add muscle to your wordpress site
Gail villanueva   add muscle to your wordpress siteGail villanueva   add muscle to your wordpress site
Gail villanueva add muscle to your wordpress site
references
 
PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)
Buck Woolley
 

Ähnlich wie EAV Sytem- Magento EAV Model (20)

Session 2 django material for training at baabtra models
Session 2 django material for training at baabtra modelsSession 2 django material for training at baabtra models
Session 2 django material for training at baabtra models
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Developing a new Epsilon EMC driver
Developing a new Epsilon EMC driverDeveloping a new Epsilon EMC driver
Developing a new Epsilon EMC driver
 
20. Magento Austria meetup - EAV Principles
20. Magento Austria meetup - EAV Principles20. Magento Austria meetup - EAV Principles
20. Magento Austria meetup - EAV Principles
 
Local storage in Web apps
Local storage in Web appsLocal storage in Web apps
Local storage in Web apps
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And Representation
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Android database tutorial
Android database tutorialAndroid database tutorial
Android database tutorial
 
Gail villanueva add muscle to your wordpress site
Gail villanueva   add muscle to your wordpress siteGail villanueva   add muscle to your wordpress site
Gail villanueva add muscle to your wordpress site
 
The Ring programming language version 1.5.1 book - Part 43 of 180
The Ring programming language version 1.5.1 book - Part 43 of 180The Ring programming language version 1.5.1 book - Part 43 of 180
The Ring programming language version 1.5.1 book - Part 43 of 180
 
2018 03 27_biological_databases_part4_v_upload
2018 03 27_biological_databases_part4_v_upload2018 03 27_biological_databases_part4_v_upload
2018 03 27_biological_databases_part4_v_upload
 
Optimization in django orm
Optimization in django ormOptimization in django orm
Optimization in django orm
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)
 
DB API Usage - How to write DBAL compliant code
DB API Usage - How to write DBAL compliant codeDB API Usage - How to write DBAL compliant code
DB API Usage - How to write DBAL compliant code
 
Advanced PLSQL Optimizing for Better Performance
Advanced PLSQL Optimizing for Better PerformanceAdvanced PLSQL Optimizing for Better Performance
Advanced PLSQL Optimizing for Better Performance
 
Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
날로 먹는 Django admin 활용
날로 먹는 Django admin 활용날로 먹는 Django admin 활용
날로 먹는 Django admin 활용
 
Ruby on rails
Ruby on rails Ruby on rails
Ruby on rails
 
Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
 

Kürzlich hochgeladen

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Kürzlich hochgeladen (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 

EAV Sytem- Magento EAV Model

  • 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.
  • 19. EAV-Advantages  Flexibility.  Don’t need to change database schema when adding new attributes.  Quick to implement.
  • 20. EAV-Disadvantages  Performance problem: a lot of tables are required when making a query with EAV Model.  Find more…
  • 22. Khoa Truong Dinh mrkhoa99@gmail.com  http://www.boolfly.com/  https://github.com/mrkhoa99  https://vn.linkedin.com/in/truongdinhkhoa  http://stackoverflow.com/users/5179786/khoa-truongdinh