2016-11-17 61 views
0

我想使用闭包生成一个类别路径。但据我所知,我在将查询转换为DQL时存在问题,因为教条不支持子查询。有没有办法做到这一点或任何其他解决方法?学说:使用子查询生成树形路径

类别表: -

id name   slug 

1  Category A category-a 
2  Category B category-b 
3  Category C category-c 

category_closure表: -

ancestor_id descendant_id path_length 

4    4    0 
4    44    1 
4    53    2 
44    44    0 
44    53    1 
53    53    0 

期望的结果: -

id name   path 

3  Category C category-a/category-b/category-c 

SQL执行: -

SELECT c.id, c.name, tmp.path 
FROM category c 
INNER JOIN (
    SELECT a.descendant_id, group_concat(c1.slug 
    ORDER BY a.path_length DESC 
    SEPARATOR '/') AS path 
    FROM category c1 
    JOIN category_closure a ON c1.id = a.ancestor_id 
    WHERE a.descendant_id = 3 
) tmp ON c.id = tmp.descendant_id 

我的教训协会如下: -

AppBundle\Entity\Category: 
    type: entity 
    table: category 
    repositoryClass: AppBundle\Repository\CategoryRepository 
    oneToMany: 
     closure: 
      targetEntity: CategoryClosure 
      mappedBy: category 

AppBundle\Entity\CategoryClosure: 
    type: entity 
    table: category_closure 
    manyToOne: 
     category: 
      targetEntity: Category 
      inversedBy: closure 
      joinColumn: 
       name: descendant_id 
       referencedColumnName: id 
  1. 是我的查询优化?
  2. 如何使用doctrine编写此查询?

任何帮助,非常感谢。谢谢

+0

我们不是通过DQL来完成这项工作,而是加载所有类别,然后在自定义存储库中构建每个类别的路径。这不是你可以遵循的方法吗? – LBA

+0

@LBA我正在考虑遵循相同的方法,找不到任何其他出路。感谢您的时间 :) – codeit

回答