教义2.0 NativeQuery ResultSetMapping为计数(ID)(Doctrine

2019-10-16 17:31发布

我生成NativeQuery的SQL,这是结果的SQL:

SELECT COUNT(号),TOT,created_at来自物业WHERE created_at '之间的201205.01亿' 与 '20120521235959' GROUP BY YEAR(created_at),MONTH(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上面的查询结果是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别名不存在!

问题是:

如何我设置正确ResultSetMapping的计数(ID)?

Answer 1:

我会尝试:

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

为您的列被改为COUNT(id) AS tot 。 如果删除tot别名,你可以使用你原来的代码:

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


文章来源: Doctrine 2.0 NativeQuery ResultSetMapping for count(id)