我正在尝试将此(我的)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')
问:是否可以使用查询构建器创建此类查询,还是应该使用本机查询?
答案 0 :(得分: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();
答案 1 :(得分:0)
有相同的主题: Link to google group