[英]Doctrine: Generate a tree path using a sub-query
我正在嘗試使用閉合表生成類別路徑。 但是據我所知,我將查詢轉換為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
任何幫助是極大的贊賞。 謝謝
您可以使用DoctrineExtensions->樹來構建閉包並利用存儲庫方法。 https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#repository-methods 。 我鼓勵您也看一下來源。
其他解決方案是使用原則本機查詢。 另一種解決方案是在實體管理器對象上的純連接上進行操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.