Mohammad Haj-Salem
Magento database
Principles of the EAV system and implementation in
Magento!
Who is Mohammad Haj-Salem?
❏ Software engineer graduated from Damascus university in 2010
❏ Web developer
❏ Oracle solutions consultant
❏ ODI/ EDQ data integration - data quality Specialist
❏ ETL/ELT processes driven by SOA(Service-oriented architecture)
❏ PL/SQL processes
❏ Database designer
❏ Technical analyst
❏ Magento Developer for almost one and half
a year in E-CONOMIX. (Nov. 2015)
Experiences
Brainstorming
❏ The first problem an analyst faces is: What attributes do I need for each
entity?!
❏ Do I have fixed number of attributes?!
❏ If I was able to include all attributes, will all of them have values or I'll get
a mega null values table?!
❏ Is it applicable to have all needed attributes in e-commerce area ?!
Database modelling
A database model shows the logical structure of a database, including the
relationships and constraints that determine how data can be stored and
accessed.
Some types of database models
❏ Hierarchical database model
❏ Relational model // most common model
❏ Network model
❏ Object-oriented database model
❏ Entity-relationship model
❏ Document model
❏ EAV model
❏ Star schema
❏ NoSQL database models
Magento uses EAV
What’s EAV?
EAV
source:http://devdocs.magento.com/
What is EAV
Wikipedia defines EAV as
Entity-Attribute-Value model (EAV), also known as object-attribute-value model and
open schema is a data model that is used in circumstances where the number of
attributes (properties, parameters) that can be used to describe a thing (an "entity" or
"object") is potentially very vast, but the number that will actually apply to a given entity
is relatively modest. In mathematics, this model is known as a sparse matrix.
source:http://devdocs.magento.com/
Facts
Source: Magento CE 1.9.2.2 database diagram
❏ Magento CE 1.9.2.2 initial installation has 333 tables!
❏ 19 tables start with core_*
❏ 47 tables start with sales_*
❏ 84 tables start with catalog_*
M2 Facts
❏ Magento CE 2.1.3 initial installation has 315 tables
❏ 1 table starts with core_*
❏ 38 tables start with sales_*
❏ 76 tables start with catalog_*
Source: Magento CE 2.1.3 database diagram
Why Magento uses EAV
Magento uses EAV database model for easy upgrade and development as
this model gives more flexibility to play with data and attributes.
❏ No alter tables scripts needed
❏ No broken ORM after upgrade
❏ EAV gives developers more control over attributes, data and how to group them
EAV vs Row modelling
❏ Sparseness of attributes
❏ In EAV model one row describes one attribute on the other hand the
row modelling one row describes one entity
❏ In row model new attribute means new column “(DDL) Data Definition
Language” , in EAV new attribute is new row “(DML) Data Manipulation
Language”
Products
product_id
prod_name
price
new_attribute
Product_id prod_name price new_attribute ….
1 Prod1 10 nA1
2 Prod2 20 nA2
EAV vs Row modelling
attribute_id = attribute_id
attributes
attribute_id name data_type
1 prod_name varchar
2 price decimal
entity
entity_id column_1
1 column_value
2 c_v_2
attribute_values_varchar
value_id attribute_id entity_id value
1 1 1 prod1
2 1 2 prod2
entity_id = entity_id
attribute_values_decimal
value_id attribute_id entity_id value
1 2 1 10
2 2 2 20
EAV vs Row modelling
❏ Performance wise
❏ row modelling is faster as far as a query runs on one table
❏ EAV modelling can be SQL intensive, this why you have to be accurate adding
attributes to select in Magento (we’re going to talk about this part later on.)
Magento uses the EAV model
Magento uses the EAV model
❏ eav_attribute table contains all attributes
❏ The name of the attribute is saved as attribute_code
❏ entity_type_id a foreign key from the table eav_entity_type
attribute_id smallint
entity_type_id smallint
attribute_code varchar
.. ..
Magento uses the EAV model
❏ eav_entity_type table contains all entity types in Magento
entity_type_id smallint
entity_type_code varchar(50)
entity_model varchar(255)
.. ..
Magento uses the EAV model
Is he going to go through all those tables?!
Nope!
But, it worth mentioning
{entity_type_code}_entity
{entity_type_code}_entity_{dataType}
How to create EAV Model in Magento
How to create EAV Model in Magento
❏ Define a model in config.xml
<global>
<!-- ... -->
<models>
<!-- ... -->
<complexworld>
<class>Alanstormdotcom_Complexworld_Model</class>
<resourceModel>complexworld_resource_eav_mysql4</resourceModel>
<resourceModel>complexworld_resource_eav_resource</resourceModel>
</complexworld>
<!-- ... -->
</models>
<!-- ... -->
</global>
❏ Notice the <resourceModel/> node
❏ EAV: {model}_resource_eav_mysql4
❏ Normal: {model}_mysql4 was deprecated after 1.6.x.x
❏ Notice: {model}_mysql4 =>{model}_resource
source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
How to create EAV model in Magento - usual steps
❏ inform Magento about your resource
<models>
<!-- ... -->
<complexworld_resource_eav_mysql4>
<class>Alanstormdotcom_Complexworld_Model_Resource_Eav_Mysql4</class>
<class>Alanstormdotcom_Complexworld_Model_Resource_Eav_Resource</class>
<entities>
<eavblogpost>
<table>eavblog_posts</table>
</eavblogpost>
</entities>
</complexworld_resource_eav_mysql4>
<!-- ... -->
</models>
source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
How to create EAV Model in Magento - Create your classes
❏ Model class
class Alanstormdotcom_Complexworld_Model_Eavblogpost extends
Mage_Core_Model_Abstract {
protected function _construct()
{
$this->_init('complexworld/eavblogpost');
}
}
❏ Model’s resource classes
class Alanstormdotcom_Complexworld_Model_Resource_Eav_Mysql4_Eavblogpost extends
Mage_Eav_Model_Entity_Abstract
{
public function _construct()
{
$resource = Mage::getSingleton('core/resource');
$this->setType('complexworld_eavblogpost'); //entity_type_code
$this->setConnection(
$resource->getConnection('complexworld_read'),
$resource->getConnection('complexworld_write')
);
}
}
EAV:Mage_Eav_Model_Entity_Abstract
Normal: Mage_Core_Model_Resource_Db_Abstract (+)
source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
How to create EAV Model in Magento - setup script
In order to make our Module working we need to insert Row in eav_entity_type,
eav_attribute_group and eav_attribute_set, which can be manual or
Automated
source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
How to create EAV model in Magento - setup script
❏ Create setup script
<resources>
<!-- ... -->
<complexworld_setup>
<setup>
<module>Alanstormdotcom_Complexworld</module>
<class>Alanstormdotcom_Complexworld_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</complexworld_setup>
<!-- ... -->
</resources>
❏ setup classes will be extending Mage_Eav_Model_Entity_Setup rather than
Mage_Core_Model_Resource_Setup
class Alanstormdotcom_Complexworld_Entity_Setup extends
Mage_Eav_Model_Entity_Setup {
source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
How to create EAV Model in Magento - Configure new Entity
❏ In the setup class
public function getDefaultEntities()
{
return array (
'complexworld_eavblogpost' => array(
'entity_model' => 'complexworld/eavblogpost',
'attribute_model' => '',
'table' => 'complexworld/eavblogpost',
'attributes' => array(
'title' => array(
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Title',
'input' => 'text',
'class' => '',
'source' => '',
'global' => 0,
'visible' => true,
'required' => true,
'user_defined' => true,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
),
),
)
);
}
source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
After running the script we’ll get new
row in eav_attribute and
eav_entity_attribute
How to create EAV model in Magento - Data set/get
❏ Set a value
$weblog2 = Mage::getModel('complexworld/eavblogpost');
$weblog2->set{AttributeCode}(‘value’);
$weblog2->setTitle(‘value’);
$weblog2->save();
❏ Get a value
$weblog2 = Mage::getModel('complexworld/eavblogpost');
$entries = $weblog2->getCollection()
->addAttributeToSelect('{AttributeCode}');
$entries = $weblog2->getCollection()
->addAttributeToSelect('title');
Take care by using
$weblog2->getCollection()->addAttributeToSelect(*);
source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
Really flexible
Even though it sometimes causes headache
❏ Performance
❏ Changes in datatype
❏ Manual imports
❏ 3rd Party User Complexity
The End!
@mhajsalem
https://www.linkedin.com/in/mhajsalem/
https://www.xing.com/profile/Mohammad_HAJSALEM

20. Magento Austria meetup - EAV Principles

  • 1.
    Mohammad Haj-Salem Magento database Principlesof the EAV system and implementation in Magento!
  • 2.
    Who is MohammadHaj-Salem? ❏ Software engineer graduated from Damascus university in 2010 ❏ Web developer ❏ Oracle solutions consultant ❏ ODI/ EDQ data integration - data quality Specialist ❏ ETL/ELT processes driven by SOA(Service-oriented architecture) ❏ PL/SQL processes ❏ Database designer ❏ Technical analyst ❏ Magento Developer for almost one and half a year in E-CONOMIX. (Nov. 2015) Experiences
  • 3.
    Brainstorming ❏ The firstproblem an analyst faces is: What attributes do I need for each entity?! ❏ Do I have fixed number of attributes?! ❏ If I was able to include all attributes, will all of them have values or I'll get a mega null values table?! ❏ Is it applicable to have all needed attributes in e-commerce area ?!
  • 4.
    Database modelling A databasemodel shows the logical structure of a database, including the relationships and constraints that determine how data can be stored and accessed. Some types of database models ❏ Hierarchical database model ❏ Relational model // most common model ❏ Network model ❏ Object-oriented database model ❏ Entity-relationship model ❏ Document model ❏ EAV model ❏ Star schema ❏ NoSQL database models
  • 5.
    Magento uses EAV What’sEAV? EAV source:http://devdocs.magento.com/
  • 6.
    What is EAV Wikipediadefines EAV as Entity-Attribute-Value model (EAV), also known as object-attribute-value model and open schema is a data model that is used in circumstances where the number of attributes (properties, parameters) that can be used to describe a thing (an "entity" or "object") is potentially very vast, but the number that will actually apply to a given entity is relatively modest. In mathematics, this model is known as a sparse matrix. source:http://devdocs.magento.com/
  • 7.
    Facts Source: Magento CE1.9.2.2 database diagram ❏ Magento CE 1.9.2.2 initial installation has 333 tables! ❏ 19 tables start with core_* ❏ 47 tables start with sales_* ❏ 84 tables start with catalog_*
  • 8.
    M2 Facts ❏ MagentoCE 2.1.3 initial installation has 315 tables ❏ 1 table starts with core_* ❏ 38 tables start with sales_* ❏ 76 tables start with catalog_* Source: Magento CE 2.1.3 database diagram
  • 9.
    Why Magento usesEAV Magento uses EAV database model for easy upgrade and development as this model gives more flexibility to play with data and attributes. ❏ No alter tables scripts needed ❏ No broken ORM after upgrade ❏ EAV gives developers more control over attributes, data and how to group them
  • 10.
    EAV vs Rowmodelling ❏ Sparseness of attributes ❏ In EAV model one row describes one attribute on the other hand the row modelling one row describes one entity ❏ In row model new attribute means new column “(DDL) Data Definition Language” , in EAV new attribute is new row “(DML) Data Manipulation Language” Products product_id prod_name price new_attribute Product_id prod_name price new_attribute …. 1 Prod1 10 nA1 2 Prod2 20 nA2
  • 11.
    EAV vs Rowmodelling attribute_id = attribute_id attributes attribute_id name data_type 1 prod_name varchar 2 price decimal entity entity_id column_1 1 column_value 2 c_v_2 attribute_values_varchar value_id attribute_id entity_id value 1 1 1 prod1 2 1 2 prod2 entity_id = entity_id attribute_values_decimal value_id attribute_id entity_id value 1 2 1 10 2 2 2 20
  • 12.
    EAV vs Rowmodelling ❏ Performance wise ❏ row modelling is faster as far as a query runs on one table ❏ EAV modelling can be SQL intensive, this why you have to be accurate adding attributes to select in Magento (we’re going to talk about this part later on.)
  • 13.
  • 14.
    Magento uses theEAV model ❏ eav_attribute table contains all attributes ❏ The name of the attribute is saved as attribute_code ❏ entity_type_id a foreign key from the table eav_entity_type attribute_id smallint entity_type_id smallint attribute_code varchar .. ..
  • 15.
    Magento uses theEAV model ❏ eav_entity_type table contains all entity types in Magento entity_type_id smallint entity_type_code varchar(50) entity_model varchar(255) .. ..
  • 16.
    Magento uses theEAV model Is he going to go through all those tables?! Nope!
  • 17.
    But, it worthmentioning {entity_type_code}_entity {entity_type_code}_entity_{dataType}
  • 18.
    How to createEAV Model in Magento
  • 19.
    How to createEAV Model in Magento ❏ Define a model in config.xml <global> <!-- ... --> <models> <!-- ... --> <complexworld> <class>Alanstormdotcom_Complexworld_Model</class> <resourceModel>complexworld_resource_eav_mysql4</resourceModel> <resourceModel>complexworld_resource_eav_resource</resourceModel> </complexworld> <!-- ... --> </models> <!-- ... --> </global> ❏ Notice the <resourceModel/> node ❏ EAV: {model}_resource_eav_mysql4 ❏ Normal: {model}_mysql4 was deprecated after 1.6.x.x ❏ Notice: {model}_mysql4 =>{model}_resource source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
  • 20.
    How to createEAV model in Magento - usual steps ❏ inform Magento about your resource <models> <!-- ... --> <complexworld_resource_eav_mysql4> <class>Alanstormdotcom_Complexworld_Model_Resource_Eav_Mysql4</class> <class>Alanstormdotcom_Complexworld_Model_Resource_Eav_Resource</class> <entities> <eavblogpost> <table>eavblog_posts</table> </eavblogpost> </entities> </complexworld_resource_eav_mysql4> <!-- ... --> </models> source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
  • 21.
    How to createEAV Model in Magento - Create your classes ❏ Model class class Alanstormdotcom_Complexworld_Model_Eavblogpost extends Mage_Core_Model_Abstract { protected function _construct() { $this->_init('complexworld/eavblogpost'); } } ❏ Model’s resource classes class Alanstormdotcom_Complexworld_Model_Resource_Eav_Mysql4_Eavblogpost extends Mage_Eav_Model_Entity_Abstract { public function _construct() { $resource = Mage::getSingleton('core/resource'); $this->setType('complexworld_eavblogpost'); //entity_type_code $this->setConnection( $resource->getConnection('complexworld_read'), $resource->getConnection('complexworld_write') ); } } EAV:Mage_Eav_Model_Entity_Abstract Normal: Mage_Core_Model_Resource_Db_Abstract (+) source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
  • 22.
    How to createEAV Model in Magento - setup script In order to make our Module working we need to insert Row in eav_entity_type, eav_attribute_group and eav_attribute_set, which can be manual or Automated source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
  • 23.
    How to createEAV model in Magento - setup script ❏ Create setup script <resources> <!-- ... --> <complexworld_setup> <setup> <module>Alanstormdotcom_Complexworld</module> <class>Alanstormdotcom_Complexworld_Entity_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </complexworld_setup> <!-- ... --> </resources> ❏ setup classes will be extending Mage_Eav_Model_Entity_Setup rather than Mage_Core_Model_Resource_Setup class Alanstormdotcom_Complexworld_Entity_Setup extends Mage_Eav_Model_Entity_Setup { source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
  • 24.
    How to createEAV Model in Magento - Configure new Entity ❏ In the setup class public function getDefaultEntities() { return array ( 'complexworld_eavblogpost' => array( 'entity_model' => 'complexworld/eavblogpost', 'attribute_model' => '', 'table' => 'complexworld/eavblogpost', 'attributes' => array( 'title' => array( 'type' => 'varchar', 'backend' => '', 'frontend' => '', 'label' => 'Title', 'input' => 'text', 'class' => '', 'source' => '', 'global' => 0, 'visible' => true, 'required' => true, 'user_defined' => true, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'unique' => false, ), ), ) ); } source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/ After running the script we’ll get new row in eav_attribute and eav_entity_attribute
  • 25.
    How to createEAV model in Magento - Data set/get ❏ Set a value $weblog2 = Mage::getModel('complexworld/eavblogpost'); $weblog2->set{AttributeCode}(‘value’); $weblog2->setTitle(‘value’); $weblog2->save(); ❏ Get a value $weblog2 = Mage::getModel('complexworld/eavblogpost'); $entries = $weblog2->getCollection() ->addAttributeToSelect('{AttributeCode}'); $entries = $weblog2->getCollection() ->addAttributeToSelect('title'); Take care by using $weblog2->getCollection()->addAttributeToSelect(*); source:http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1/
  • 26.
    Really flexible Even thoughit sometimes causes headache ❏ Performance ❏ Changes in datatype ❏ Manual imports ❏ 3rd Party User Complexity
  • 27.