EAV : Entity - Attribute - Value 的缩写,是数据库模型的一种,使用eav建模的好处是可以动态为数据模型增加或移除属性。
1. 问题提出:
假设需要定义一个实体Customer的信息,通常我们只要定义一个表为customer,并定义相应的属性即可。倘若某天需要为customer增加一个新的属性如“毕业学校”,那么就需要更改表的结构。
如果使用EAV模型则不必改变表结构。
2. Magento的EAV模型定义:
在Magento中,EAV模型相关的表定义有:
Java代码
eav_attribute
eav_attribute_group
eav_attribute_option
eav_attribute_option_value
eav_attribute_set
eav_entity
eav_entity_attribute
eav_entity_datetime
eav_entity_decimal
eav_entity_int
eav_entity_store
eav_entity_text
eav_entity_type
eav_entity_varchar
eav_attribute
eav_attribute_group
eav_attribute_option
eav_attribute_option_value
eav_attribute_set
eav_entity
eav_entity_attribute
eav_entity_datetime
eav_entity_decimal
eav_entity_int
eav_entity_store
eav_entity_text
eav_entity_type
eav_entity_varchar
现在让我来观察最重要的三张表
eav_entity_type,eav_entity_attribute,eav_attribute
1) eav_entity_type表用来定义实体的基本信息。
Sql代码
mysql>select*fromeav_entity_typewhereentity_type_id=1;
+----------------+------------------+-------------------+-----------------+-----------------+
| entity_type_id | entity_type_code | entity_model | attribute_model | entity_table | +----------------+------------------+-------------------+-----------------+-----------------+
| 1 | customer | customer/customer | | customer/entity |
+----------------+------------------+-------------------+-----------------+-----------------+
mysql> select * from eav_entity_type where entity_type_id=1;
+----------------+------------------+-------------------+-----------------+-----------------+
| entity_type_id | entity_type_code | entity_model | attribute_model | entity_table | +----------------+------------------+-------------------+-----------------+-----------------+
| 1 | customer | customer/customer | | customer/entity |
+----------------+-----------