2016-06-14 44 views
0

我需要成績單這種要求的原則下symfon:具體的SQL查詢主義翻譯

SELECT node.name 
FROM nested_category AS node, 
     nested_category AS parent 
WHERE node.lft BETWEEN parent.lft AND parent.rgt 
     AND parent.name = 'ELECTRONICS' 
ORDER BY node.lft; 

我嘗試這一點,但它不工作:

$nodesDQL = $this->createQueryBuilder('childs') 
     ->select('childs') 
     ->from('AppBundle:NestedCategory', 'parent') 
     ->join('AppBundle:NestedCategory', 'childs') 
     ->where(new BetweenExpression('childs.lft', 'parent.lft', 'parent.right')) 
     ->andWhere('parent = :parent') 
     ->setParameter('parent', $node); 

我可以」像這樣加入,任何想法都歡迎!

我想到subrequest,但在教條中如何?

問候。 PS:我英語不好,英語有困難。

+0

我想我需要使用學說關係並添加更多信息,如深度或父母的ID? –

回答

1

我不認爲你需要加入父母,如果每個孩子已經有父母身份,它可以只是一個標準。

E.g.

$qb = $this->createQueryBuilder('children'); 

    $qb->select('children') 
     ->from('AppBundle:NestedCategory', 'children') 
     ->where($qb->expr()->between('children.lft', ':parentLft', ':parentRgt')) 
     ->andWhere('children.parent = :parent') 
     ->setParameter('parent', $node) 
     ->setParameter('parentLft', $node->getLft()) // assuming you can get lft/rgt from parent 
     ->setParameter('parentRgt', $node->getRgt()) 
     ->getQuery() 
     ->getResult() 
    ; 

這會讓你的父節點的所有子節點,其中子節點留在父母之間的權利/左。

+0

這似乎是好的,但我猜笛卡爾產品怎麼樣? –