Doctrine 2.0 NativeQuery ResultSetMapping for count(id) [英] Doctrine 2.0 NativeQuery ResultSetMapping for count(id)
问题描述
SELECT COUNT(id)AS tot,created_at FROM property WHERE create_at
BETWEEN'20120501000000'AND'20120521235959'GROUP BY
YEAR(created_at),MONTH(created_at),DAY(created_at);
这是我的ResultSetMapping:
$ rsm = new \Doctrine\ORM\Query\\ \\ResultSetMapping;
$ rsm-> addEntityResult('Entity\Property','p');
$ rsm-> addFieldResult('p','count(id)','tot'); //< - 这不工作
$ rsm-> addFieldResult('p','created_at','created_at');
运行上述查询直接在mysql上的结果是可以的:
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别名不存在!!
问题是:
如何正确设置ResultSetMapping为 count(id)?
我会尝试:
$ rsm-> addFieldResult('p' 'tot','tot');
作为您重命名的列与 COUNT (id)AS tot
。如果删除 tot
别名,您可以使用原始代码:
$ rsm-> addFieldResult('p','COUNT(id)','tot');
I generate an SQL from NativeQuery and this is the result 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);
This is my ResultSetMapping:
$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');
The result running the above query direct on mysql is 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
But the result array the tot alias don't exist!!
The Question is:
How i set ResultSetMapping correctly for count(id)?
I would try:
$rsm->addFieldResult('p', 'tot', 'tot');
as your column as been renamed with COUNT(id) AS tot
. If remove the tot
alias you can use your original code:
$rsm->addFieldResult('p', 'COUNT(id)', 'tot');
这篇关于Doctrine 2.0 NativeQuery ResultSetMapping for count(id)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!