Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Array Results in findAll() - Doctrine?

I need to fetch all records in database as Array using findAll() in Doctrine, My Query is Something like this

$result = $this->getDoctrine()
                ->getRepository('CoreBundle:Categories')
                ->findAll(\Doctrine\ORM\Query::HYDRATE_ARRAY);

even if set Hydration Mode to HYDRATE_ARRAY, am getting results as objects

array:4 [▼
0 => Categories {#323 ▶}
1 => Categories {#326 ▶}
2 => Categories {#329 ▶}
3 => Categories {#332 ▶}
]

what mistake i made?

like image 345
Hari Avatar asked Mar 20 '17 17:03

Hari


2 Answers

The findAll() method does not have any parameters. You can, for example, use the repository's createQueryBuilder() method to achieve what you want to do:

use Doctrine\ORM\Query;

// ...

$query = $this->getDoctrine()
    ->getRepository('CoreBundle:Categories')
    ->createQueryBuilder('c')
    ->getQuery();
$result = $query->getResult(Query::HYDRATE_ARRAY);
like image 173
xabbuh Avatar answered Nov 15 '22 06:11

xabbuh


It's possible to use $query->getArrayResult() as a shortcut to $query->getResult(Query::HYDRATE_ARRAY)

doctrine hydration modes

like image 7
Pascal KOCH Avatar answered Nov 15 '22 06:11

Pascal KOCH