18

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

3 Answers 3

46

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);
Sign up to request clarification or add additional context in comments.

3 Comments

i am having the same porblem and its giving me this error "Error: Class 'apb\appassBundle\Controller\Doctrine\ORM\Query' not found in controller"
Use a leading slash on \Doctrine\ORM\Query
thanx it was also working when i added use class and just add Query::HYDRATE_ARRAY
28

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();

Comments

-1

Use getScalarResult() to get an array result with objects truncated to strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.