2011-08-15 29 views
4

我已經在原則2寫過DQL查詢:如何從DQL查詢中返回對象?

$qb->select('r.position') 
    ->from('\Entities\Races', 'r') 
    ->where($qb->expr()->eq('r.entrantId', ':entrant_id')) 
    ->setParameter('entrant_id', $this->entrantId); 
$query = $qb->getQuery(); 
$aRaces = $query->getResult(); 

目前,它返回的查詢結果中的數組,像這樣:

Array 
(
    [0] => Array 
     (
      [position] => 10 
     ) 

    [1] => Array 
     (
      [position] => 4 
     ) 
) 

我想要的結果返回競賽的數組對象,這樣我就可以訪問與對象關聯的方法(我很確定默認情況下Doctrine的先前版本返回對象)。

我想:

$aRaces = $query->getResult(Query::HYDRATE_OBJECT); 

但是這並沒有有所作爲。

欣賞的幫助

回答

4

您從DB只讀取position列。嘗試用select(r)替換select('r.position')。見DQL reference

如果你需要的對象,只有position屬性,是指partial objects

+0

感謝夥計,那排序它。 – Sid

0

我沒能解決我的問題,你的解決方案,我在這裏的部分代碼:

$qb = $this->_objectManager->createQuery('Select d from Hotbed\Entity\Department d where d.id <> :id and d.title = :title'); 
     $qb->setParameters(array('id' => $context['id'], 'title' => $value)); 
     $match = $qb->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT); 

$匹配返回此:

array(1) { 
[0] => object(Hotbed\Entity\Department)#626 (4) { 
    ['inputFilter':protected] =&gt; NULL 
    ['id':protected] =&gt; int(25) 
    ['title':protected] => string(4) '2222' 
    ['state':protected] => int(0) 
} 
} 

THX任何幫助

2

的:$qb->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);

返回與對象的數組,你可以趕上他們:$match[0];

如果你想返回一個結果,你必須使用:$qb->getOneOrNullResult()