我有這個疑問:主義resultsetmapping應該返回數組
select count(*) as total_count, sum(total) as total_value, status from invoice group by status;
當我在數據庫中,我得到下面的執行:
我想達到的使用Doctrine2相同的,但以下代碼僅返回一行:
$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;
這是上面的結果:
array (size=1)
0 =>
array (size=3)
'totalCount' => string '1432' (length=1)
'totalValue' => string '55447.80999999999' (length=2)
'status' => string 'paid' (length=4)
嘗試用'$查詢 - >的getResult( AbstractQuery :: HYDRATE_SINGLE_SCALAR);' – Matteo
@Matteo that give me'查詢返回一個包含多列的行。改變查詢或者使用不同的結果函數,比如getScalarResult()。' – Jonathan
我認爲doctrine會爲你提取多行。試試'getSingleResult()'。請參閱[文檔](http://docs.doctrine-project.org/projects/doctrine-phpcr-odm/en/latest/reference/query.html)。 – ferdynator