2017-03-02 58 views
0

我想选择没有提供给QueryBuilder#select(...)的实体。在原始的SQL它应该是这样的:如何使用Doctrine的QueryQuilder通过另一个实体类型选择实体?

SELECT x.* 
FROM aaa a 
INNER JOIN bbb b ON a.document_id = b.id 
INNER JOIN ccc c ON b.client_user_id = c.id 
INNER JOIN xxx x ON c.client_id = x.id 
WHERE a.id = 123 

现在我想实现这个逻辑与QueryBuilder

$query = $queryBuilder->select('x') 
    ->from(Aaa::class, 'a') 
    ->join('a.bbb', 'b') 
    ->join('b.ccc', 'c') 
    ->join('c.xxx', 'x') 
    ->where('a.id = :aId') 
    ->setParameter('aId', $aId) 
    ->getQuery() 
; 

但它不工作:

[Semantical Error] line 0, col -1 near 'SELECT x FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias.

如何与Doctrine的QueryQuilder一起工作?

回答

0

不幸的是,这是不可能与学说(见Benjamin Eberlei陈述在GitHub这个问题)。

溶液/解决方法是从Xxx限定的方向上的整个协会链Aaa然后找到以下的“路径”在开始Xxx实体FROMXxx

$query = $queryBuilder->select('x') 
    ->from(Xxx::class, 'x') 
    ->join('x.ccc', 'c') 
    ->join('c.bbb', 'b') 
    ->join('b.aaa', 'a') 
    ->where('a.id = :aId') 
    ->setParameter('aId', $aId) 
    ->getQuery() 
;