我正在开发一个 Symfony2 项目(Doctrine),我想从 ResultSetMapping 对象中获取一个简单的关联数组。
这是我的要求:
$rsm = new ResultSetMapping;
//$rsm->addIndexByScalar('name');
$rsm->addScalarResult('name', 'name');
$rsm->addScalarResult('rate', 'rate');
return $this->_em->createNativeQuery(sprintf('
SELECT DISTINCT t1.sold_drug as name, (COUNT(*)/(SELECT COUNT(*) FROM Transaction t WHERE t.sold_drug != ""))*100 as rate
FROM Transaction t1
WHERE t1.sold_drug != "" AND t1.prescribed_but_not_sold != "No"
GROUP BY name
ORDER BY rate DESC', $this->getFilterQuery()), $rsm)->getResult();
所以我想得到一个关联数组,例如:
array('nameA' => 10, 'nameB' => 2, ...);
我尝试添加 ScalarResult / ScalarIndex 但没有成功!
任何想法 ?谢谢 !