2015-05-31 79 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') 

注:將隱藏關鍵字有助於獲得唯一的實體後面,這使得水化適合在

相關問題