0

我从 NativeQuery 生成一个 SQL,这是结果 sql:

SELECT COUNT(id) AS tot,created_at FROM property WHERE created_at BETWEEN '20120501000000' AND '20120521235959' GROUP BY YEAR(created_at),MONTH(created_at),DAY(created_at);

这是我的结果集映射:

$rsm = new \Doctrine\ORM\Query\ResultSetMapping;
$rsm->addEntityResult('Entity\Property', 'p');
$rsm->addFieldResult('p','count(id)','tot'); // <-- this don't work
$rsm->addFieldResult('p','created_at','created_at');

直接在mysql上运行上面的查询结果是ok的:

tot created_at
11  2012-05-02 11:23:25
11  2012-05-08 14:56:40
152 2012-05-16 14:43:46
5   2012-05-17 16:26:31
29  2012-05-18 14:55:33

但是结果数组 tot 别名不存在!!

问题是:

我如何为count(id)正确设置 ResultSetMapping ?

4

1 回答 1

0

我会尝试:

$rsm->addFieldResult('p', 'tot', 'tot');

因为您的列已重命名COUNT(id) AS tot. 如果删除tot别名,您可以使用原始代码:

$rsm->addFieldResult('p', 'COUNT(id)', 'tot');
于 2012-05-29T12:49:03.023 回答