2015-05-31 150 views
0

目标:订购如何我的实体ofter它们在存在关联关系中使用(一对多)学说:排序按计数对相关联的实体

问题: DQL,它以某种方式处理FROM关键字是一个实体或上课。

抛出的异常: QueryException:[语义错误] line 0 col 103'FROM MyBundle:Entity Error:Class'FROM'未定义。


这是我写的测试如何获取数据的SQL查询。它完美

SELECT en.id, (COUNT(DISTINCT ag.artist_id) + COUNT(DISTINCT rg.release_id) + COUNT(DISTINCT tg.track_id)) AS total 

FROM myapp.entity AS en 

LEFT JOIN myapp.other_one AS o1 ON o1.entity_id = en.id 
LEFT JOIN myapp.other_two AS o2 ON o2.entity_id = en.id 

GROUP BY en.id 

ORDER BY total DESC ; 

要获得symfony的数据来滋润对象我尝试使用Doctrine查询语言在EntityRepository这样的:

/** 
* Find Most Common Entities 
* 
* @return array 
*/ 
public function findMostCommon() 
{ 
    $em = $this->getEntityManager(); 
    $qb = $em->createQueryBuilder(); 

    $qb 
     ->select('en, (COUNT(DISTINCT en.other1) + COUNT(DISTINCT en.other2)) AS count') 
     ->from('MarulaBundle:Entity', 'en') 
     ->leftJoin('MyBundle:Other1', 'o1', 'WITH', 'o1.entity = en.id') 
     ->leftJoin('MyBundle:Other2', 'o2', 'WITH', 'o2.entity = en.id') 
     ->groupBy('en') 
     ->orderBy('count', 'DESC') 
    ; 

    return $qb->getQuery()->getResult(); 
} 

由于可以在一个SQL查询中。我希望它能和DQL一样好。

有没有人遇到过这个错误?是否有可能通过教义来实现这一点,或者是否有关于这个问题的教条限制?

回答

1

OOPS:我不应该使用关键字“计数”

解决方案:

->select('en, (COUNT(DISTINCT en.other1) + COUNT(DISTINCT en.other2)) AS HIDDEN orderCount') 

注:将隐藏关键字有助于获得唯一的实体后面,这使得水化适合在

相关问题