Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return array, not object from Doctrine query - Symfony2

I'm using this:

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

I thought that should ensure it returns an array of an array, but it still returns an array of objects.

I need the whole result returned as an array of an array so I can do this kind of thing (silly example, but it explains what I mean):

<?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']; ?>
like image 800
user2143356 Avatar asked Jul 06 '13 01:07

user2143356


2 Answers

According to this EntityRepository class, findAll don't take multiple arguments.

The code below should do what you want

$result = $this->getDoctrine()
               ->getRepository('MyBundle:MyEntity')
               ->createQueryBuilder('e')
               ->select('e')
               ->getQuery()
               ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
like image 176
Thomas Potaire Avatar answered Nov 04 '22 09:11

Thomas Potaire


You can also use the getArrayResult() function instead of getResult(). It returns an array of data instead:

$query = $em->createQuery("SELECT test FROM namespaceTestBundle:Test test");
$tests = $query->getArrayResult();
like image 27
Samir Mengadi Avatar answered Nov 04 '22 09:11

Samir Mengadi