我想在Doctrine原生查询中使用SUM函数,但是我总是得到空的结果。这是我的代码:
$em = $this->getDoctrine()->getManager();
$rsm = new ResultSetMapping();
$rsm->addEntityResult('AndreiStatisticsBundle:Visit', 'v');
$rsm->addScalarResult('counter', 'counter');
$rsm->addScalarResult('created_at', 'created_at');
$query = $em->createNativeQuery(
'SELECT SUM(counter) FROM visits GROUP BY created_at',
$rsm
);很有趣,因为如果我不使用SUM函数,代码就会工作:
$em = $this->getDoctrine()->getManager();
$rsm = new ResultSetMapping();
$rsm->addEntityResult('AndreiStatisticsBundle:Visit', 'v');
$rsm->addScalarResult('counter', 'counter');
$rsm->addScalarResult('created_at', 'created_at');
$query = $em->createNativeQuery(
'SELECT counter FROM visits GROUP BY created_at',
$rsm
);有人能查出我错过了什么吗?
发布于 2013-11-14 10:04:19
你能试试这个吗?
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('AndreiStatisticsBundle:Visit');
$qb = $repository->createQueryBuilder('v');
$qb->select('SUM(v.counter) AS counterSum');
$qb->groupBy('v.created_at');
$count = $qb->getQuery()->getSingleScalarResult();https://stackoverflow.com/questions/19973933
复制相似问题