Magento的EAV模型窥探

EAV : Entity - Attribute - Value 的缩写,是数据库模型的一种,使用eav建模的好处是可以动态为数据模型增加或移除属性。

1. 问题提出:

假设需要定义一个实体Customer的信息,通常我们只要定义一个表为customer,并定义相应的属性即可。倘若某天需要为customer增加一个新的属性如“毕业学校”,那么就需要更改表的结构。
如果使用EAV模型则不必改变表结构。

2. Magento的EAV模型定义:

在Magento中,EAV模型相关的表定义有:
Java代码 复制代码  收藏代码
  1. eav_attribute                
  2. eav_attribute_group          
  3. eav_attribute_option         
  4. eav_attribute_option_value   
  5. eav_attribute_set            
  6. eav_entity                   
  7. eav_entity_attribute         
  8. eav_entity_datetime          
  9. eav_entity_decimal           
  10. eav_entity_int               
  11. eav_entity_store             
  12. eav_entity_text              
  13. eav_entity_type              
  14. 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代码 复制代码  收藏代码
  1. mysql> select * from  eav_entity_type where entity_type_id=1;   
  2. +----------------+------------------+-------------------+-----------------+-----------------+   
  3. | entity_type_id | entity_type_code | entity_model      | attribute_model | entity_table    | +----------------+------------------+-------------------+-----------------+-----------------+   
  4. |              1 | customer         | customer/customer |                 | customer/entity |   
  5. +----------------+------------------+-------------------+-----------------+-----------------+  
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 |
+----------------+------------------+-------------------+-----------------+-----------------+


2). eav_entity_attribute表用来定义实体customer模型包含哪些属性
Sql代码 复制代码  收藏代码
  1. mysql> select * from eav_entity_attribute  where entity_type_id='1';   
  2. +---------------------+----------------+------------------+--------------------+--------------+   
  3. | entity_attribute_id | entity_type_id | attribute_set_id | attribute_group_id | attribute_id |   
  4. +---------------------+----------------+------------------+--------------------+--------------+   
  5. |                   1 |              1 |                1 |                  1 |            1 |   
  6. |                   2 |              1 |                1 |                  1 |            2 |   
  7. |                   3 |              1 |                1 |                  1 |            3 |   
  8. |                   4 |              1 |                1 |                  1 |            4 |   
  9. |                   5 |              1 |                1 |                  1 |            5 |   
  10. |                   6 |              1 |                1 |                  1 |            6 |   
  11. |                   7 |              1 |                1 |                  1 |            7 |   
  12. |                   8 |              1 |                1 |                  1 |            8 |   
  13. |                   9 |              1 |                1 |                  1 |            9 |   
  14. |                  10 |              1 |                1 |                  1 |           10 |   
  15. |                  11 |              1 |                1 |                  1 |           11 |   
  16. |                  12 |              1 |                1 |                  1 |           12 |   
  17. |                  13 |              1 |                1 |                  1 |           13 |   
  18. |                  14 |              1 |                1 |                  1 |           14 |   
  19. |                  15 |              1 |                1 |                  1 |           15 |   
  20. |                  16 |              1 |                1 |                  1 |           16 |   
  21. +---------------------+----------------+------------------+--------------------+--------------+  
mysql> select * from eav_entity_attribute  where entity_type_id='1';
+---------------------+----------------+------------------+--------------------+--------------+
| entity_attribute_id | entity_type_id | attribute_set_id | attribute_group_id | attribute_id |
+---------------------+----------------+------------------+--------------------+--------------+
|                   1 |              1 |                1 |                  1 |            1 |
|                   2 |              1 |                1 |                  1 |            2 |
|                   3 |              1 |                1 |                  1 |            3 |
|                   4 |              1 |                1 |                  1 |            4 |
|                   5 |              1 |                1 |                  1 |            5 |
|                   6 |              1 |                1 |                  1 |            6 |
|                   7 |              1 |                1 |                  1 |            7 |
|                   8 |              1 |                1 |                  1 |            8 |
|                   9 |              1 |                1 |                  1 |            9 |
|                  10 |              1 |                1 |                  1 |           10 |
|                  11 |              1 |                1 |                  1 |           11 |
|                  12 |              1 |                1 |                  1 |           12 |
|                  13 |              1 |                1 |                  1 |           13 |
|                  14 |              1 |                1 |                  1 |           14 |
|                  15 |              1 |                1 |                  1 |           15 |
|                  16 |              1 |                1 |                  1 |           16 |
+---------------------+----------------+------------------+--------------------+--------------+


3), 由上表推出customer实体包含16个属性,下面的语句查看每个属性的具体定义
Sql代码 复制代码  收藏代码
  1. mysql> select * from eav_attribute where attribute_id<=16 and attribute_id>=1 ;   
  2. +--------------+----------------+------------------+--------------+   
  3. | attribute_id | entity_type_id | attribute_code   | backend_type |   
  4. +--------------+----------------+------------------+--------------+   
  5. |            1 |              1 | website_id       | static       |   
  6. |            2 |              1 | store_id         | static       |   
  7. |            3 |              1 | created_in       | varchar      |   
  8. |            4 |              1 | prefix           | varchar      |   
  9. |            5 |              1 | firstname        | varchar      |   
  10. |            6 |              1 | middlename       | varchar      |   
  11. |            7 |              1 | lastname         | varchar      |   
  12. |            8 |              1 | suffix           | varchar      |   
  13. |            9 |              1 | email            | static       |   
  14. |           10 |              1 | group_id         | static       |   
  15. |           11 |              1 | dob              | datetime     |   
  16. |           12 |              1 | password_hash    | varchar      |   
  17. |           13 |              1 | default_billing  | int          |   
  18. |           14 |              1 | default_shipping | int          |   
  19. |           15 |              1 | taxvat           | varchar      |   
  20. |           16 |              1 | confirmation     | varchar      |   
  21. +--------------+----------------+------------------+--------------+  
mysql> select * from eav_attribute where attribute_id<=16 and attribute_id>=1 ;
+--------------+----------------+------------------+--------------+
| attribute_id | entity_type_id | attribute_code   | backend_type |
+--------------+----------------+------------------+--------------+
|            1 |              1 | website_id       | static       |
|            2 |              1 | store_id         | static       |
|            3 |              1 | created_in       | varchar      |
|            4 |              1 | prefix           | varchar      |
|            5 |              1 | firstname        | varchar      |
|            6 |              1 | middlename       | varchar      |
|            7 |              1 | lastname         | varchar      |
|            8 |              1 | suffix           | varchar      |
|            9 |              1 | email            | static       |
|           10 |              1 | group_id         | static       |
|           11 |              1 | dob              | datetime     |
|           12 |              1 | password_hash    | varchar      |
|           13 |              1 | default_billing  | int          |
|           14 |              1 | default_shipping | int          |
|           15 |              1 | taxvat           | varchar      |
|           16 |              1 | confirmation     | varchar      |
+--------------+----------------+------------------+--------------+


所以,使用上述的模型,一旦有CUSTOMER属性定义的添加和删除,只需要增加或删除 eav_entity_attribute的记录即可

3. 使用EAV模型
现在,在Magento系统注册一个新的用户,看看实例数据如何存放在数据库
Sql代码 复制代码  收藏代码
  1. mysql> select * from customer_entity            ;   
  2. +-----------+----------------+------------------+------------+--------------------+   
  3. | entity_id | entity_type_id | attribute_set_id | website_id | email              |   
  4. +-----------+----------------+------------------+------------+--------------------+   
  5. |         1 |              1 |                0 |          1 | koda.guo@gmail.com |   
  6. +-----------+----------------+------------------+------------+--------------------+   
  7.   
  8. mysql> select * from customer_entity_varchar    ;   
  9. +----------+----------------+--------------+-----------+-------------------------------------+   
  10. | value_id | entity_type_id | attribute_id | entity_id | value                               |   
  11. +----------+----------------+--------------+-----------+-------------------------------------+   
  12. |        1 |              1 |            5 |         1 | Koda                                |   
  13. |        2 |              1 |            7 |         1 | Guo                                 |   
  14. |        4 |              1 |            3 |         1 | Default Store View                  |   
  15. |        5 |              1 |           12 |         1 | 2256e441b74ab3454a41c821f5de1e9d:9s |   
  16. +----------+----------------+--------------+-----------+-------------------------------------+  
mysql> select * from customer_entity            ;
+-----------+----------------+------------------+------------+--------------------+
| entity_id | entity_type_id | attribute_set_id | website_id | email              |
+-----------+----------------+------------------+------------+--------------------+
|         1 |              1 |                0 |          1 | koda.guo@gmail.com |
+-----------+----------------+------------------+------------+--------------------+

mysql> select * from customer_entity_varchar    ;
+----------+----------------+--------------+-----------+-------------------------------------+
| value_id | entity_type_id | attribute_id | entity_id | value                               |
+----------+----------------+--------------+-----------+-------------------------------------+
|        1 |              1 |            5 |         1 | Koda                                |
|        2 |              1 |            7 |         1 | Guo                                 |
|        4 |              1 |            3 |         1 | Default Store View                  |
|        5 |              1 |           12 |         1 | 2256e441b74ab3454a41c821f5de1e9d:9s |
+----------+----------------+--------------+-----------+-------------------------------------+


从上表看到customer_entity 和customer_entity_varchar用来存放相应属性的实际输入值。如:
Koda, Guo分别属性编号5,7即firstname和lastname实际值

和customer实体定义相对应的实例存放的相关表包括:
Java代码 复制代码  收藏代码
  1. customer_entity             
  2. customer_entity_datetime    
  3. customer_entity_decimal     
  4. customer_entity_int         
  5. customer_entity_text        
  6. customer_entity_varchar    
customer_entity          
customer_entity_datetime 
customer_entity_decimal  
customer_entity_int      
customer_entity_text     
customer_entity_varchar  


总结
了解Magento的EAV模型结构是扩展Magento必须的知识。 其意义在于,我们常常需要扩展Magento某些实体的属性,或者创建自定义的eav模型实例。希望本文对你有所启示。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值