Magento的表有三百多张,以实体、属性、值(EAV)的数据库结构难以掌握,加上缺少有关EAV的文档,以至许多人不知道这种EAV方式的好处以及它对magento来说的重要性,在这里作为一名magento开发者,让我们来了解下,它是如何工作的并且对我们有什么好处。
什么是EAV呢?
EAV是实体(Entity)、属性(Attribute)、值(Value)的意思,接下来来看看每一部分以便更好的理解它。
实体(Entity)
实体指的是magento的数据对象,如产品、分类目录、客户、订单等,每一个实体在数据库中都对应着一条实体记录。
属性(Attribute)
属性是指跟实体相关的一些性质数据,如产品实体有名称、价格、状态等。
值(Value)
值是最容易理解的了,就是指属性的值了。
EAV是怎么工作的呢?
一直以来,数据库其实很简单的,比如我们现在要设计一个商城,需要有一张产品表,包括所有产品的信息,另一张表包括分类信息,也许还要一张表来连接这两张,这样很容易理解吧,然而magento却不一样,它跟产品以及分类有关的表有40多张,要想知道为什么,让我们来看下产品表。
不像其它的商城那样,所有的产品信息在一张表里,magento把产品信息分离在子表中,最顶上的表是catalog_product_entity,如果你看过这张表,你肯定发现了,它只包括产品的一些基础信息,除了SKU,其它你看不到任何有用的信息,幸运地是使用这张表你将可以从属性和值表中看到完整的产品记录。
让我们开始新建一条完整的产品记录,你需要将属性与我们的实体表相关联,做这之前先看下表eav_attribute,这张表在magento里为所有不同的实体存储了所有的属性,打开表,你会看到里面有好几百条不同属性的记录,为什么有些名称还是一样的呢?困惑吧?magento是如何辨别的呢?很快你就会注意到entity_type_id,每一个实体都会有一个entity_type_id,为了找出来,那就再回来catalog_product_entity表,看entity_type_id字段,你会发现所有的记录值都是10,如果你有去看catalog_category_entity,你将会看到一个不同的entity_type_id值。根据这个值和attribute code你就可以找到所有产品的属性,当然也可以所有其它实体的属性了。
思考下下面的查询:
# 找出所有产品的属性
SELECT attribute_code FROM eav_attribute WHERE entity_type_id = 10;
# 找出单个产品的属性
SELECT attribute_code FROM eav_attribute WHERE entity_type_id = 10 AND attribute_code = 'name';
你得到属性和实体了吧。接下来了解下值,值被分离在不同的表中,让我们看下所有前缀是catalog_product_entity的表,值是根据它们的类型来分的,例如,所有的价格以及其它decimal属性的会存储在表catalog_product_entity_decimal中,另外所有文本类型数据会存储在catalog_product_varchar中,需要指出的是每个属性存储的表,magento在eav_attribute表中使用字段backend_type记录,如果你运行以下查询,你将可以找到产品属性’name’的backend type。
SELECT attribute_code, backend_type FROM eav_attribute WHERE entity_type_id = 4 AND attribute_code = 'name';
希望以上的查询返回的是varchar,这就是name的正确类型啦,基于以上,我们可以知道namer值被存储在表catalog_product_entity_varchar中,
/*
*Weicot 兔子属性测试
*属性回溯 通过SKU 追踪SKU Color 属性
*作者 ajiang-兔子
*20151223
*1050653098@qq.com
*/
产品主表
catalog_product_entity
mysql> select * from catalog_product_entity limit 0,10 \G;
*************************** 1. row ***************************
entity_id: 24
entity_type_id: 4
attribute_set_id: 4
type_id: simple
sku: 291194
has_options: 1
required_options: 1
created_at: 2015-09-18 06:56:48
updated_at: 2015-10-15 10:37:48
*************************** 2. row ***************************
查找产品属性
SELECT attribute_code FROM eav_attribute WHERE entity_type_id = 4 AND attribute_code ='color';
通过 entity_id 查找产品配置
select * from catalog_product_entity_int where entity_id='11';
mysql> select * from catalog_product_entity_int where entity_id='11';
+----------+----------------+--------------+----------+-----------+-------+
| value_id | entity_type_id | attribute_id | store_id | entity_id | value |
+----------+----------------+--------------+----------+-----------+-------+
| 34 | 4 | 81 | 0 | 11 | NULL |
| 35 | 4 | 92 | 0 | 11 | 6 |
| 31 | 4 | 96 | 0 | 11 | 1 |
| 37 | 4 | 100 | 0 | 11 | 0 |
| 32 | 4 | 102 | 0 | 11 | 4 |
| 33 | 4 | 121 | 0 | 11 | 0 |
| 36 | 4 | 134 | 0 | 11 | 5 |
+----------+----------------+--------------+----------+-----------+-------+
//通过SKU 获得 entity_id
select * from catalog_product_entity where sku='292592';
mysql> select * from catalog_product_entity where sku='292592' \G
*************************** 1. row ***************************
entity_id: 1130
entity_type_id: 4
attribute_set_id: 4
type_id: simple
sku: 292592
has_options: 1
required_options: 1
created_at: 2015-12-22 08:54:30
updated_at: 2015-12-22 08:54:30
1 row in set (0.22 sec)
//获得 entity_id 1130 配置数据
select * from catalog_product_entity_int where entity_id='1130' \G
mysql> select * from catalog_product_entity_int where entity_id='1130';
+----------+----------------+--------------+----------+-----------+-------+
| value_id | entity_type_id | attribute_id | store_id | entity_id | value |
+----------+----------------+--------------+----------+-----------+-------+
| 68662 | 4 | 92 | 0 | 1130 | 3 |
| 68665 | 4 | 96 | 0 | 1130 | 1 |
| 68667 | 4 | 102 | 0 | 1130 | 4 |
| 68666 | 4 | 121 | 0 | 1130 | 0 |
| 68664 | 4 | 132 | 0 | 1130 | 6 |
| 68663 | 4 | 133 | 0 | 1130 | 9 |
| 68701 | 4 | 138 | 1 | 1130 | 1 |
+----------+----------------+--------------+----------+-----------+-------+
//获得color attribute_id 92
SELECT * FROM eav_attribute WHERE entity_type_id = 4 AND attribute_code ='color'\G
mysql> SELECT * FROM eav_attribute WHERE entity_type_id = 4 AND attribute_code ='color'\G
*************************** 1. row ***************************
attribute_id: 92
entity_type_id: 4
attribute_code: color
attribute_model: NULL
backend_model: NULL
backend_type: int
backend_table: NULL
frontend_model: NULL
frontend_input: select
frontend_label: Color
frontend_class: NULL
source_model: NULL
is_required: 0
is_user_defined: 1
default_value: 6
is_unique: 0
note: NULL
1 row in set (0.20 sec)
//获得 entity_type_id: 4 的值
select * from eav_entity_type \G;
mysql> select * from eav_entity_type \G;
*************************** 3. row ***************************
entity_type_id: 3
entity_type_code: catalog_category
entity_model: catalog/category
attribute_model: catalog/resource_eav_attribute
entity_table: catalog/category
value_table_prefix: NULL
entity_id_field: NULL
is_data_sharing: 1
data_sharing_key: default
default_attribute_set_id: 3
increment_model: NULL
increment_per_store: 0
increment_pad_length: 8
increment_pad_char: 0
additional_attribute_table: catalog/eav_attribute
entity_attribute_collection: catalog/category_attribute_collection
*************************** 4. row ***************************
entity_type_id: 4
entity_type_code: catalog_product
entity_model: catalog/product
attribute_model: catalog/resource_eav_attribute
entity_table: catalog/product
value_table_prefix: NULL
entity_id_field: NULL
is_data_sharing: 1
data_sharing_key: default
default_attribute_set_id: 4
increment_model: NULL
increment_per_store: 0
increment_pad_length: 8
increment_pad_char: 0
additional_attribute_table: catalog/eav_attribute
entity_attribute_collection: catalog/product_attribute_collection
//获得 attribute_id='92'; 的集合
mysql> select * from eav_attribute_option where attribute_id='92';
+-----------+--------------+------------+
| option_id | attribute_id | sort_order |
+-----------+--------------+------------+
| 3 | 92 | 3 |
| 4 | 92 | 2 |
| 5 | 92 | 1 |
| 12 | 92 | 4 |
| 13 | 92 | 6 |
| 14 | 92 | 5 |
| 18 | 92 | 7 |
+-----------+--------------+------------+
//选项列表
select * from eav_attribute_option_value;
mysql> select * from eav_attribute_option_value;
+----------+-----------+----------+-------------------+
| value_id | option_id | store_id | value |
+----------+-----------+----------+-------------------+
| 1 | 1 | 0 | Male |
| 2 | 2 | 0 | Female |
| 39 | 9 | 0 | Elite |
| 40 | 9 | 1 | Elite |
| 41 | 10 | 0 | Limited |
| 42 | 10 | 1 | Limited |
| 43 | 11 | 0 | Game |
| 44 | 11 | 1 | Game |
| 100 | 19 | 0 | Game |
| 101 | 19 | 1 | Game |
| 102 | 20 | 0 | Limited |
| 103 | 20 | 1 | Limited |
| 104 | 21 | 0 | Hoodie |
| 105 | 21 | 1 | Hoodie |
| 106 | 22 | 0 | Elite |
| 107 | 22 | 1 | Elite |
| 114 | 26 | 0 | Hoodie |
| 115 | 27 | 0 | elite |
| 117 | 29 | 0 | Mitchell and Ness |
| 151 | 31 | 0 | Infant |
| 152 | 31 | 1 | Infant |
| 153 | 28 | 0 | men |
| 154 | 6 | 0 | Men |
| 155 | 6 | 1 | Men |
| 156 | 7 | 0 | Women |
| 157 | 7 | 1 | Women |
| 158 | 8 | 0 | Youth |
| 159 | 8 | 1 | Youth |
| 160 | 16 | 0 | Toddler |
| 161 | 16 | 1 | Toddler |
| 200 | 42 | 0 | Sweater |
| 201 | 30 | 0 | C Patch |
| 202 | 25 | 0 | C patch |
| 203 | 25 | 1 | C patch |
| 204 | 24 | 0 | SB XLVIII |
| 205 | 24 | 1 | SB XLVIII |
| 206 | 23 | 0 | SB XLIX |
| 207 | 23 | 1 | SB XLIX |
| 208 | 43 | 0 | Lights Out |
| 209 | 44 | 0 | Drift |
| 210 | 32 | 0 | Camo |
| 211 | 33 | 0 | Salute to Service |
| 212 | 34 | 0 | Split |
| 213 | 38 | 0 | Zebra |
| 298 | 5 | 0 | Blue |
| 299 | 5 | 1 | Blue |
| 300 | 4 | 0 | Grey |
| 301 | 4 | 1 | Grey |
| 302 | 3 | 0 | White |
| 303 | 3 | 1 | White |
| 304 | 12 | 0 | Green |
| 305 | 12 | 1 | Green |
| 306 | 14 | 0 | Black |
| 307 | 14 | 1 | Black |
| 308 | 13 | 0 | Red |
| 309 | 13 | 1 | Red |
| 310 | 18 | 0 | Pink |
| 311 | 18 | 1 | Pink |
+----------+-----------+----------+-------------------+
58 rows in set (0.21 sec)
转载请注明:(●--●) Hello.My Weicot » Magento数据库结构 EAV 之 属性回溯 通过SKU 追踪SKU Color 属性