Doctrine 2.0 NativeQuery ResultSetMapping for count(id)

时间:2012-05-21 19:09:18

标签: sql mapping doctrine-orm

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

  

SELECT COUNT(id)AS tot,created_at FROM property WHERE created_at   '20120501000000'与'20120521235959'BROUP BY之间的比较   YEAR(created_at),月(created_at),日(created_at);

这是我的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');

直接在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

但结果数组的别名不存在!!

问题是:

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

1 个答案:

答案 0 :(得分:0)

我会尝试:

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

将您的列重命名为并添加COUNT(id) AS tot。如果删除tot别名,则可以使用原始代码:

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