[英]Doctrine: Generate a tree path using a sub-query
I'm trying to generate a category path using a closure table. 我正在尝试使用闭合表生成类别路径。 But I'm having a problem converting my query into DQL as doctrine doesn't support sub-queries, as far as I know. 但是据我所知,我将查询转换为DQL时遇到问题,因为该学说不支持子查询。 Is there a way to do it or any other workaround? 有没有办法做到这一点或任何其他解决方法?
category table :- 类别表 :-
id name slug
1 Category A category-a
2 Category B category-b
3 Category C category-c
category_closure table :- 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
Desired result :- 预期结果 :-
id name path
3 Category C category-a/category-b/category-c
SQL executed :- 执行的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
My doctrine associations are as follows:- 我的学说协会如下:
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
Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks 谢谢
You can use DoctrineExtensions -> tree to build closure and take advantage of repository methods. 您可以使用DoctrineExtensions->树来构建闭包并利用存储库方法。 https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#repository-methods . https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#repository-methods 。 I encourage you also to take a look at the source. 我鼓励您也看一下来源。
Other sollution is to use doctrine native query. 其他解决方案是使用原则本机查询。 Another sollution is to operate on pure connection on entity manager object. 另一种解决方案是在实体管理器对象上的纯连接上进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.