对于 magento EAV 的理解

7天成为Magento系统架构师,现在开始学习Magento全栈开发!

《Magento2.X企业级开发实战》

最近在研究magento 的EAV 模型 至于什么事EAV呐:
EAV模型是Zend框架的基础,而Magento项目又是建立在Zend框架的基础上的,所有了解EAV有助于了解Magento的架构原理,在开发Magento相关应用时非常有用。

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

1. 问题提出:
假设需要定义一个实体Customer的信息,通常我们只要定义一个表为customer,并定义相应的属性即可。倘若某天需要为customer增加一

个新的属性如“毕业学校”,那么就需要更改表的结构。
如果使用EAV模型则不必改变表结构。

2. Magento的EAV模型定义:
在Magento中,EAV模型相关的表定义有:

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

备注 这里面的表来自 这两篇文章高级Magento模型 EAV,和Creating EAV based model(s) in Magento-高级Magento模型 EAV
1) eav_entity_type表用来定义实体的基本信息。

Sql代码

mmysql> select * from eav_entity_type where entity_type_id=9 G
*************************** 1. row ***************************
             entity_type_id: 9
           entity_type_code: weicot_eavblogpost
               entity_model: weicot-eav/eavblogpost
            attribute_model:
               entity_table: weicot-eav/blogpost
         value_table_prefix: NULL
            entity_id_field: NULL
            is_data_sharing: 1
           data_sharing_key: default
   default_attribute_set_id: 0
            increment_model:
        increment_per_store: 0
       increment_pad_length: 8
         increment_pad_char: 0
 additional_attribute_table: NULL
entity_attribute_collection: NULL
1 row in set (0.00 sec)

2). eav_entity_attribute表用来定义实体weicot_eavblogpost模型包含哪些属性

Sql代码

mysql> select * from eav_entity_attribute where entity_type_id=9 ;
+---------------------+----------------+------------------+--------------------+--------------+------------+
| entity_attribute_id | entity_type_id | attribute_set_id | attribute_group_id | attribute_id | sort_order |
+---------------------+----------------+------------------+--------------------+--------------+------------+
|                 130 |              9 |                9 |                 18 |          133 |          1 |
|                 131 |              9 |                9 |                 18 |          134 |          2 |
+---------------------+----------------+------------------+--------------------+--------------+------------+
2 rows in set (0.00 sec)

3), 由上表推出weicot_eavblogpost实体包含2个属性,下面的语句查看每个属性的具体定义

Sql代码

mysql> select  *  from  eav_attribute  where  attribute_id=132 G
*************************** 1. row ***************************
   attribute_id: 132
 entity_type_id: 9
 attribute_code: title
attribute_model: NULL
  backend_model: NULL
   backend_type: varchar
  backend_table: NULL
 frontend_model: NULL
 frontend_input: text
 frontend_label: Title
 frontend_class: NULL
   source_model: NULL
    is_required: 1
is_user_defined: 1
  default_value: NULL
      is_unique: 0
           note: NULL
*************************** 2. row ***************************
   attribute_id: 133
 entity_type_id: 9
 attribute_code: content
attribute_model: NULL
  backend_model: NULL
   backend_type: text
  backend_table: NULL
 frontend_model: NULL
 frontend_input: textarea
 frontend_label: Content
 frontend_class: NULL
   source_model: NULL
    is_required: 1
is_user_defined: 0
  default_value: NULL
      is_unique: 0
           note: NULL
*************************** 3. row ***************************
   attribute_id: 134
 entity_type_id: 9
 attribute_code: date
attribute_model: NULL
  backend_model: NULL
   backend_type: datetime
  backend_table: NULL
 frontend_model: NULL
 frontend_input: datetime
 frontend_label: Post Date
 frontend_class: NULL
   source_model: NULL
    is_required: 0
is_user_defined: 0
  default_value: NULL
      is_unique: 0
           note: NULL
3 rows in set (0.00 sec)

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

3. 使用EAV模型
现在,在Magento系统添加一个新的留言,看看实例数据如何存放在数据库

Sql代码

mmysql> SELECT * FROM eavblog_posts ORDER BY entity_id DESC;
+-----------+----------------+------------------+--------------+----------+---------------------+---------------------

+-----------+
| entity_id | entity_type_id | attribute_set_id | increment_id | store_id | created_at          | updated_at          | 

is_active |
+-----------+----------------+------------------+--------------+----------+---------------------+---------------------

+-----------+
|        29 |              9 |                0 |              |        0 | 2015-11-02 09:25:07 | 2015-11-02 09:25:07 |   

      1 |
|        28 |              9 |                0 |              |        0 | 2015-11-02 09:25:07 | 2015-11-02 09:25:07 |   

      1 |
|        27 |              9 |                0 |              |        0 | 2015-11-02 09:23:21 | 2015-11-02 09:23:21 |   

      1 |
|        26 |              9 |                0 |              |        0 | 2015-11-02 09:23:21 | 2015-11-02 09:23:21 |   

      1 |
+-----------+----------------+------------------+--------------+----------+---------------------+---------------------

+-----------+
4 rows in set (0.00 sec)
mysql> select * from  eavblog_posts_varchar;
+----------+----------------+--------------+----------+-----------+-------------------------------+
| value_id | entity_type_id | attribute_id | store_id | entity_id | value                         |
+----------+----------------+--------------+----------+-----------+-------------------------------+
|       26 |              9 |          132 |        0 |        26 | Hellow My Weicot NO.0         |
|       27 |              9 |          132 |        0 |        27 | Hellow My Weicot NO.1         |
|       28 |              9 |          132 |        0 |        28 | Mail 1050653098@qq.com - NO.0 |
|       29 |              9 |          132 |        0 |        29 | Mail 1050653098@qq.com - NO.1 |
+----------+----------------+--------------+----------+-----------+-------------------------------+

写入方式:

$eavblog->setTitle('Hellow My Weicot NO.'.$i);

4 rows in set (0.00 sec)

从上表看到eavblog_posts和eavblog_posts_varchar用来存放相应属性的实际输入值。如:
Hellow My Weicot NO.0 属性编号132 即实际值 title
和weicot_eavblogpost实体定义相对应的实例存放的相关表包括:
这种

 eavblog_posts
 eavblog_posts_char
 eavblog_posts_datetime
 eavblog_posts_decimal
 eavblog_posts_int
 eavblog_posts_text
 eavblog_posts_varchar

备注以及总结
1.了解Magento的EAV模型结构是扩展Magento必须的知识。 其意义在于,我们常常需要扩展Magento某些实体的属性,或者创建自定义的eav

模型实例。希望本文对你有所启示
2.由于我写了两个安装模块 可能是忘了删掉其中一个 然后导致了文章里有奇怪的东西 还请 多多指正
参考资料
高级Magento模型 EAV
Creating EAV based model(s) in Magento-高级Magento模型 EAV
EAV模型 三 [Entity-Attribute-Value,实体-属性-值]

转载请注明:(●–●) Hello.My Weicot » 对于 magento EAV 的理解

文章来源于互联网:对于 magento EAV 的理解

如无特殊说明或标注,任何个人或组织,复制、转载、采集本站内容请注明:
本文来源于:【Magento中文网】,并添加本文地址链接。
如未按上述操作复制或转载,本站有权追究法律责任。
若本站内容侵犯了原著者的合法权益,可联系我们进行处理。