我有一个名为的邻接表(为了便于递归attribute,它还有一个名为 mapped 的闭包表)。attribute_closure
表中的每个条目attribute都是 4 种分层类型之一,每种类型都可以继承和覆盖其父类型的条目。按层次结构,四种可能的类型是category、product_line、product、model。所以category定义了一个属性树,它继承并且可以在任何时候覆盖,product_line等等。productmodel
这是预先存在的应用程序的预先存在的结构,因此任何重组建议都是不可用的:-)
因此,邻接列表attribute具有以下列:id, parent_id, overrides_id,overrides_id(如果设置)是对 的引用attribute.id,对于 也是如此parent_id。如果overrides_id设置,parent_id将始终匹配被覆盖属性的值parent_id。
对于每个层次类型,都有一个将类型映射到属性的支持表,即 - category_id, attribute_id。
我需要能够取回完整的属性树,并尊重所有覆盖。
示例数据(此特定示例仅涉及产品级别,但您明白了)。请根据需要使用您自己的示例数据进一步充实。
attribute
+-------+-----------+--------------+
| id | parent_id | overrides_id |
+-------+-----------+--------------+
| 6036 | 5931 | NULL |
| 6069 | 5931 | 6036 |
| 30955 | 5931 | 6069 |
+-------+-----------+--------------+
category_attribute
+-------------+--------------+
| category_id | attribute_id |
+-------------+--------------+
| 2 | 6036 |
+-------------+--------------+
product_line_attribute
+-----------------+--------------+
| product_line_id | attribute_id |
+-----------------+--------------+
| 16 | 6069 |
+-----------------+--------------+
product_attribute
+------------+--------------+
| product_id | attribute_id |
+------------+--------------+
| 69 | 30955 |
+------------+--------------+
查询包含上述属性的树,应该只30955返回属性 id,因为显示的其他 2 个属性应该在 30955 之前被废弃。
如前所述,我还有一个典型的闭包表,它映射出ancestor, descendant, level. 如果您可以包含使用闭包返回具有覆盖已生效的树的结果,则额外加分。:-)