request with multiple id Symfony2 Doctrine

Ajouve picture Ajouve · Nov 6, 2013 · Viewed 54.8k times · Source

I've got an array of IDs and I'd like to get an entity array from my IDs array.

I can't use find.

The sql request looks like:

SELECT * FROM mytable WHERE id = 12 OR id = 10 ...

with a loop on my id array.

Answer

Piotr Pasich picture Piotr Pasich · Nov 6, 2013

You can also get it directly from repository:

$em->getRepository('YourRepo')->findById(array(1,2,3,4,5));

Also you can pass parameters in get no tin array, but in simple string glued by commas

?ids=1,2,3,4,56

And after that get it from $request

$em->getRepository('YourRepo')->findById(explode(',', $request->get('ids'));