Doctrine 2.0 NativeQuery ResultSetMapping for count(id) [英] Doctrine 2.0 NativeQuery ResultSetMapping for count(id)

查看:239
本文介绍了Doctrine 2.0 NativeQuery ResultSetMapping for count(id)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆