2

我正在尝试将此(My)SQL 转换为 DQL

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

而且我被困在 GROUP BY 部分,显然 DAY/WEEK/MONTH 未被识别为有效的“功能”。

[语义错误] 第 0 行,第 80 列“(p.created)ORDER”附近:错误:无法按未定义的标识变量分组。

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

问:是否可以使用查询生成器创建这种查询,或者我应该使用本机查询?

4

2 回答 2

5

在 Doctrine 2.1.? 的 GROUP BY 查询中无法使用自定义 DAY/WEEK/MONTH 用户函数,仅支持 SELECT 查询(不确定 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();
于 2012-05-29T10:03:37.230 回答
0

有相同的主题: Link to google group

于 2012-05-23T13:36:11.770 回答