Symfony 2 Doctrine find by ordered array of id

Benjamin picture Benjamin · Feb 17, 2015 · Viewed 40.1k times · Source

I'm looking for a way to use Doctrine in Symfony 2 to find items using an ordered array of id.

I have a Card entity with id (primary key) and title.

I have a ListCards entity with id (primary key) and a listCards (an array of ids encoded : ["16", "2", "84"])

I first fetch the list and then I need to find cards with those ids in that order.

I try something like :

$idsArray = ["16", "2", "84"];
$cardRepository->findby($idsArray);

but Doctrine fetch my cards in ASC order.

ORDER BY FIEDS sql method doesn't seem to be supported by doctrine.

Is there any simple solution for that kind of sorting ?

Thank you (and sorry for my bad english).

Answer

Max Lipsky picture Max Lipsky · Feb 18, 2015

You can use it like:

$cardRepository->findBy( array('id' => $idsArray), array('id' => 'DESC') );

Check also the official doctrine documentation for more details on how to use ordering, limit and offset as second to fourth parameters in the findBy method.