这是默认主义hydration modes不可能的。学说有可能写Custom Hydration Mode意图达到像你的目标。所以,你必须编写自定义的水化模式:
#app/config/config.yml
doctrine:
orm:
...
entity_managers:
default:
auto_mapping: true
hydrators:
MyHydrator: App\YourBundle\Hydrators\MyHydrator
保湿:
// App\YourBundle\Hydrators\MyHydrator.php
namespace App\YourBundle\Hydrators;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use \PDO;
class MyHydrator extends AbstractHydrator
{
protected function hydrateAllData()
{
$result = array();
$cache = array();
foreach($this->_stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$this->hydrateRowData($row, $cache, $result);
}
return $result;
}
protected function hydrateRowData(array $row, array &$cache, array &$result)
{
if(count($row) == 0) {
return false;
}
$result[] = $row['id0'];
}
}
,简单地用途:
$results = $this->getDoctrine()->getManager()
->createQuery('{yourQuery}')->getResult('MyHydrator');
,或者在你的情况下,你的实体仓库:
$qb = $this->createQueryBuilder('b')
->select('b.id')
->where('b.game = :gID')
->setParameter('gID', $game->getId());
return $qb->getQuery()->getResult('MyHydrator');
更多在这example。
来源
2014-02-24 20:40:42
NHG
谢谢,我想出了解决方案。想想也许我可以直接从教条中得到它。 – m0c
不幸的是,即使您选择单列的投影,您从数据库中获得的数据行(数组)也不是标量集。 – ziollek
他应该编写自定义水合模式。请看我的答案。 – NHG