How I can with doctrine2 order by array with ids ?
I have this query:
$qb = $this->createQueryBuilder('u')
->select('u', 'n', 'c')
->leftJoin('u.notifications', 'n')
->leftJoin('u.channel', 'c')
->andWhere('u.id IN (:ids)')
->setParameter('ids', $ids);
I want that the result has the same order that array with ids, is possible do it ?
Thanks
SOLUTION:
Use FIELD mysql extension with https://github.com/beberlei/DoctrineExtensions
:)
Thanks
Simple solution that doesn't require presorting query result:
$idPositions = array_flip($userIds); // Mapping of id to position
usort($users, function($userA, $userB) use ($idPositions) {
return $idPositions[$userA->id] - $idPositions[$userB->id];
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With