I have this query:
select count(*) as total_count, sum(total) as total_value, status from invoice group by status;
when I execute it in the database I get the following:
I want to achieve the same using Doctrine2 but the following code only returns one row:
$rsm = new ResultSetMapping();
$rsm->addScalarResult('total_count', 'totalCount');
$rsm->addScalarResult('total_value', 'totalValue');
$rsm->addScalarResult('status', 'status');
$query = $this->getObjectManager()->createNativeQuery('
select count(*) as total_count, sum(total) as total_value, status from invoice group by status', $rsm);
$result = $query->getResult();
return $result;
This is the result of the above:
array (size=1)
0 =>
array (size=3)
'totalCount' => string '1432' (length=1)
'totalValue' => string '55447.80999999999' (length=2)
'status' => string 'paid' (length=4)
You should use:
$result = $query->getScalarResult();
Instead of :
$result = $query->getResult();
Hope this help
Because you are using getResult
doctrine will try to fetch all matching rows for you. You need to use Doctrine's getSingleResult
if you don't want the result to be an array of arrays.
So for you simply:
$result = $query->getSingleResult();
You can also limit the number of results to throw a QueryException
if more or none are returned:
$query->setMaxResults(1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.