Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 Doctrine find by ordered array of id

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).

like image 382
Benjamin Avatar asked Feb 17 '15 14:02

Benjamin


1 Answers

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.

like image 100
Max Lipsky Avatar answered Oct 26 '22 07:10

Max Lipsky