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?
It's possible to use
$query->getArrayResult()
as a shortcut to$query->getResult(Query::HYDRATE_ARRAY)
doctrine hydration modes
The
findAll()
method does not have any parameters. You can, for example, use the repository'screateQueryBuilder()
method to achieve what you want to do:The format in which the result of a DQL SELECT query is returned can be influenced by a so-called
hydration mode
so you couldn't use it forfindAll()
.you may try this below:I have made this function:
https://gist.github.com/AndreiLN/3708ab829c26cee4711b1df551d1385f
If you find some upgrade please let me know.
Explaining more of this function: it's get the doctrine object of array, if it's a object it's reads all get's methods to get all values, if this values is another doctrine object (And single option is not set) it's call the function recursively until it's done. If the parameter is a array the function will go throught of it and call the method again for all it's values.
It's easy to use, but not tested in all situations.