這是默認主義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