Magento数据库中如何存储产品属性和属性选项?

33
我正在研究Magento中属性和属性选项之间的关联以及产品和属性之间是如何建立联系的。是否有关于这个问题的参考资料?或者有没有人能够给我一些提示。

谢谢,

Balan

6个回答

70
正如Alan Storm所说:“你不需要知道数据库是如何工作的。你需要学习模型是如何工作的”(这不是确切的引语,我给出了它的含义)。
但我创建了自己的方案来理解数据库结构。因此,这个屏幕显示了它的工作原理: 进入图像描述 进入图像描述 希望能帮到您。
我还建议您查看以下链接: http://www.magentocommerce.com/wiki/2_-_magento_concepts_and_architecture/magento_database_diagram http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1

18
哎呀!太糟糕了!(同时为惊人的研究点赞+1) - clockworkgeek
7
上述图片的链接,以便正确阅读:http://i.stack.imgur.com/PQsc1.png,http://i.stack.imgur.com/d65bi.png - Imran Zahoor

38
1) 属性存储在eav_attribute中。在那里您可以获得attribute_id

2) 选项存储在eav_attribute_option_value中。在那里您可以获得option_id

3) 选项分配给产品在catalog_product_entity_varchar中。在那里,您需要使用来自1)的attribute_id、产品的entity_id以及值,即来自2)的逗号分隔的option_ids

属性集如何与属性关联? - Abdul Moiz
@Abdul,请查看eav_entity_attribute表。 - stollr

6

我发现以下查询对于查找类似这样的内容非常有帮助 - 比如产品颜色在哪里显示为黑色?

-- show_product_attr.sql
select
   p.entity_id,
   p.entity_type_id,
   p.attribute_set_id,
   p.type_id,
   p.sku,
   a.attribute_id,
   a.frontend_label as attribute,
   av.value
from
   catalog_product_entity p
   left join catalog_product_entity_{datatype} av on
      p.entity_id = av.entity_id
   left join eav_attribute a on
      av.attribute_id = a.attribute_id
where
   -- p.entity_id = 28683
   -- p.sku = '0452MR'
   p.entity_id = {eid}
;

对于attr_options

-- show_product_attr_options.sql
select
   p.entity_id,
   -- p.entity_type_id,
   -- p.attribute_set_id,
   p.type_id,
   p.sku,
   a.attribute_id,
   a.frontend_label as attribute,
   -- a.attribute_code,
   av.value,
   ao.*
from
   catalog_product_entity p

   left join catalog_product_entity_int av on
      p.entity_id = av.entity_id

   left join eav_attribute a on
      av.attribute_id = a.attribute_id
   left join eav_attribute_option_value ao on
      av.value = ao.option_id 
where
   -- p.entity_id = 28683
   p.entity_id = {eid}
;

您需要在第一个查询中用text、varchar、int、decimal等替换{datatype},在两个查询中都用entity_id替换{eid}。您可以使用以下命令完成此操作:

$ cat show_product_attr_options.sql | sed -e "s/{eid}/30445/" | mysql -uUSER -pPASS DATABASE -t
+-----------+---------+--------------+--------------+---------------------------+-------+----------+-----------+----------+--------------------+-------------+
| entity_id | type_id | sku          | attribute_id | attribute                 | value | value_id | option_id | store_id | value              | colorswatch |
+-----------+---------+--------------+--------------+---------------------------+-------+----------+-----------+----------+--------------------+-------------+
|     30445 | simple  | 840001179127 |           96 | Status                    |     1 |     5972 |         1 |        0 | Male               | NULL        |
|     30445 | simple  | 840001179127 |          102 | Visibility                |     1 |     5972 |         1 |        0 | Male               | NULL        |
|     30445 | simple  | 840001179127 |          122 | Tax Class                 |     2 |     5973 |         2 |        0 | Female             | NULL        |
|     30445 | simple  | 840001179127 |          217 | Size                      |   257 |    17655 |       257 |        0 | XS                 | NULL        |
|     30445 | simple  | 840001179127 |          217 | Size                      |   257 |    17657 |       257 |        1 | XS                 | NULL        |
|     30445 | simple  | 840001179127 |          224 | Color                     |   609 |    18717 |       609 |        0 | Arctic Ice Heather | NULL        |
|     30445 | simple  | 840001179127 |          260 | Featured                  |     0 |     NULL |      NULL |     NULL | NULL               | NULL        |
|     30445 | simple  | 840001179127 |          262 | Clearance Product         |     0 |     NULL |      NULL |     NULL | NULL               | NULL        |
|     30445 | simple  | 840001179127 |          263 | Skip from Being Submitted |     0 |     NULL |      NULL |     NULL | NULL               | NULL        |
|     30445 | simple  | 840001179127 |          283 | Discontinued              |     0 |     NULL |      NULL |     NULL | NULL               | NULL        |
+-----------+---------+--------------+--------------+---------------------------+-------+----------+-----------+----------+--------------------+-------------+

可以为目录创建一组类似的 SQL 脚本。

4
SELECT pei.value 
FROM `catalog_product_entity_int` pei 
JOIN `eav_attribute` ea 
ON pei.attribute_id = ea .attribute_id 
WHERE pei.entity_id = {your product_id} 
AND ea.attribute_code = '{your attribute_code}'

请注意,根据属性类型,类似catalog_product_entity_int的不同表格有许多个,因此其中另一个可能更合适。

3
产品属性是您可以分配给产品并存储在主EAV表中的额外值,按名称存储,然后根据数据类型(如varchar、decimal、text Integer、date等)存储在几个不同的表中。
如果您有多个产品属性的值,则将其存储在属性选项表中,再次根据数据类型使用不同的表格。
以下链接更好地解释了这些关系: http://www.magentocommerce.com/wiki/2_-_magento_concepts_and_architecture/magento_database_diagram 更深入的开发人员详细信息: http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-7-advanced-orm-entity-attribute-value

属性集是您将遇到的另一件事情,如名称所示,是一组分组在一起的属性。http://www.magentocommerce.com/knowledge-base/entry/how-do-i-create-an-attribute-set

希望对您有所帮助, Shaun


0

您可以使用以下查询获取所有产品属性:

SELECT CPEV.entity_id, CPE.sku, EA.attribute_id, EA.frontend_label, CPEV.value
    FROM catalog_product_entity_varchar AS CPEV 
    INNER JOIN catalog_product_entity AS CPE ON CPE.entity_id = CPEV.entity_id
    INNER JOIN eav_attribute AS EA ON(CPEV.attribute_id = EA.attribute_id AND EA.entity_type_id = 4)
    INNER JOIN catalog_eav_attribute AS CEA ON(CEA.attribute_id = EA.attribute_id AND CEA.is_visible_on_front = 1 AND CEA.is_visible_in_grid = 1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接