我正在为多个实体(产品,公司......)开发EAV系统 我最后会说出问题。
数据库结构如下:
EAV_ENTITY
此表格将为我提供所有可用实体的列表,例如:
1 | Product
2 | Company
结构:
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| model | varchar(255) | NO | | | |
+-------+------------------+------+-----+---------+----------------+
EAV_ATTRIBUTE
这个表有一个属性列表,ref是一个助记符,用于通过查询更容易地获取属性。我在这里添加了与表eav_entity相关的entity_type_id,因为我想将每个属性限制为只有一个实体。
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| entity_type_id | int(11) unsigned | NO | MUL | NULL | |
| ref | varchar(50) | NO | UNI | | |
| name | varchar(255) | NO | UNI | | |
+----------------+------------------+------+-----+---------+----------------+
EAV_VALUE
此表包含与属性相关的值,值为:红色,绿色,橙色,属性为“颜色”
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| attribute_id | int(11) unsigned | NO | MUL | NULL | |
| value | varchar(255) | NO | MUL | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
EAV_ATTRIBUTE_VALUE
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| entity_id | int(11) unsigned | YES | | NULL | |
| entity_type_id | int(11) unsigned | YES | MUL | NULL | |
| value_id | int(11) | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
在这里,我创造了所有的“魔力”,我在查询中建立实体(类型,id)与所有相关值以及值所属的属性之间的关系。
我的查询就是这个
SELECT
e.model,
av.entity_id,
a.id as category_id,
a.ref as category_ref,
v.id as value_id
FROM eav_entity as e
RIGHT JOIN eav_attribute_value as av
ON e.id = av.entity_type_id
LEFT JOIN eav_value as v
ON v.id = av.value_id
LEFT JOIN eav_attribute as a
ON v.attribute_id = a.id
WHERE e.model = 'App\Product'
问题&问题
当我添加语句WHERE e.model ='Whatever'时,我得不到任何结果。 它应该是过滤器。但是如果没有WHERE语句,我会得到预期的结果。 我认为这可能是与我进行连接的方式有关的问题。
没有WHERE e.model
的真实示例+-------------+-----------+-------------+---------------------------+----------+
| model | entity_id | category_id | category_ref | value_id |
+-------------+-----------+-------------+---------------------------+----------+
| App\Product | 13 | 2 | product_development_phase | 2 |
| App\Product | 13 | 2 | product_development_phase | 3 |
| App\Product | 13 | 4 | product_therapeutics | 58 |
| App\Company | 13 | 4 | product_therapeutics | 58 |
+-------------+-----------+-------------+---------------------------+----------+
答案 0 :(得分:0)
查询将产生下面的内容:
SELECT
e.model,
av.entity_id,
a.id as category_id,
a.ref as category_ref,
v.id as value_id
FROM eav_entity as e
RIGHT JOIN eav_attribute_value as av
ON e.id = av.entity_type_id
LEFT JOIN eav_value as v
ON v.id = av.value_id
LEFT JOIN eav_attribute as a
ON v.attribute_id = a.id
WHERE e.model = "App\\Product"