返回数组,而不是Doctrine查询中的对象 - Symfony2

时间:2013-07-06 01:10:30

标签: php symfony doctrine-orm doctrine

我正在使用它:

$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']; ?>

3 个答案:

答案 0 :(得分:41)

根据此EntityRepository classfindAll不会采用多个参数。

下面的代码应该做你想要的

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

答案 1 :(得分:24)

您也可以使用getArrayResult()功能代替getResult()。它返回一个数据数组:

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

答案 2 :(得分:-1)

使用getScalarResult()获取截断为字符串的对象的数组结果。