2012-05-23 165 views
2

我試圖把這種(我的)SQL來DQL翻譯SQL查詢來Doctrine2 DQL

SELECT content, created, AVG(rating) 
FROM point 
GROUP BY DAY(created) 
ORDER BY created ASC 

而且我被困在GROUP BY部分,顯然是日/周/月沒有被識別爲有效的「功能」。

[語義錯誤] line 0,col 80 near'(p.created)ORDER':錯誤:無法通過未定義的標識變量進行分組。

$this->createQueryBuilder('p') 
     ->select('p') 
     ->groupBy('DAY(p.created)') 
     ->orderBy('p.created', 'ASC') 

問:是否有可能造成這種與查詢生成器的查詢,或者我應該使用本機查詢?

+0

按照DQL文件,你就需要添加一個用戶定義的函數來獲取此功能。 –

回答

5

這是不可能的學說2.1使用自定義的天/周/月的用戶功能在GROUP BY查詢,只能選擇查詢的支持(不知道爲2.2。?分支),所以我最終使用本地查詢,並且一切正常。

代碼的快速概述:

// creating doctrines result set mapping obj. 
$rsm = new Doctrine\ORM\Query\ResultSetMapping(); 

// mapping results to the message entity 
$rsm->addEntityResult('Omglol\AppBundle\Entity\Message', 'm'); 
$rsm->addFieldResult('m', 'id', 'id'); 
$rsm->addFieldResult('m', 'content', 'content'); 
$rsm->addFieldResult('m', 'rating', 'rating'); 
$rsm->addFieldResult('m', 'created', 'created'); 

$sql = "SELECT id, content, AVG(rating) as rating, created 
     FROM message 
     WHERE domain_id = ? 
     GROUP BY WEEK(created)"; 

$query = $this->_em->createNativeQuery($sql, $rsm); 
$query->setParameter(1, $domainId); 
$query->getResult();