2013-07-06 132 views
13

我使用這個:返回數組,而不是從教義查詢對象 - Symfony2的

$this->getDoctrine()->getRepository('MyBundle:MyEntity')->findAll(array(), Query::HYDRATE_ARRAY); 

我認爲,應該確保它返回一個數組的數組,但它仍然會返回對象的數組。

我需要返回一個數組,所以我可以做這種事情的數組整個結果(愚蠢的例子,但它說明了我的意思):

<?php 
$result = $this->getDoctrine()->getRepository('MyBundle:MyEntity')->findAll('return-an-array'); 
?>  
This is the age of the person at the 5th record: <?php echo $result[4]['age']; ?> 

回答

40

根據這一EntityRepository classfindAll唐沒有多個論點。

下面的代碼應該做你想要

$result = $this->getDoctrine() 
       ->getRepository('MyBundle:MyEntity') 
       ->createQueryBuilder('e') 
       ->select('e') 
       ->getQuery() 
       ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); 
+0

我有同樣的問題,它給了我這個錯誤「Error:Class'apb \ appassBundle \ Controller \ Doctrine \ ORM \ Query'not found in controller」 – numerah

+0

在'\ Doctrine \ ORM \ Query上使用前導斜槓' –

+0

thanx它也工作時,我添加使用類,只是添加查詢:: HYDRATE_ARRAY – numerah

24

你也可以用它代替getResult()getArrayResult()功能是什麼。它返回一組數據來代替:

$query = $em->createQuery("SELECT test FROM namespaceTestBundle:Test test"); 
$tests = $query->getArrayResult(); 
0

使用getScalarResult()得到一個數組結果與對象截斷爲字符串。

相關問題