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

时间:2012-02-27 10:37:46

标签: magento

我试图找出属性和属性选项以及产品和属性之间的链接是如何在Magento中进行的。有没有提到这是如何工作的?或者任何人都给我一个暗示。

谢谢,

巴兰

6 个答案:

答案 0 :(得分:56)

正如Alan Storm所说:“你不必知道你的数据库是如何工作的。你必须了解模型是如何工作的”。 (这不是一个确切的引用。我给你的意思)。

但我创建了自己的方案来理解数据库结构。所以这个屏幕显示它是如何工作的: enter image description here enter image description here

希望,这有帮助。

我还建议您浏览以下链接:

http://www.magentocommerce.com/wiki/2_-_magento_concepts_and_architecture/magento_database_diagram

http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1

答案 1 :(得分:34)

1)属性存储在eav_attribute中。你得到attribute_id

2)选项存储在eav_attribute_option_value中。你得到option_id

3)选项分配给catalog_product_entity_varchar中的产品。在那里,您需要产品的entity_id,来自1的attribute_id和从2开始的逗号分隔option_ids的值

答案 2 :(得分:12)

每次我想知道关于magento db关系如何工作的事情我都会检查这个

online Database Diagram Tool

答案 3 :(得分:3)

产品属性是您可以分配给产品的额外值,并按名称存储在主EAV表中,然后根据数据类型将数据存储在几个不同的表中,如varchar,decimal,text整数,日期等

如果您的产品属性有多个值,那么它将再次存储在属性选项表中,基于数据类型的不同表。

以下链接更好地解释了这些关系: 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

HTH 肖恩

答案 4 :(得分:3)

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,具体取决于属性的类型,因此其中一个表可能是合适的。

答案 5 :(得分:3)

我发现这些查询非常有助于搜索诸如 - 例如产品颜色是黑色的地方吗?

-- 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脚本。