Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctrine2 order by array with ids

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

like image 304
Marc Morales Valldepérez Avatar asked May 23 '14 16:05

Marc Morales Valldepérez


1 Answers

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];
 });
like image 64
Bouke Versteegh Avatar answered Nov 03 '22 08:11

Bouke Versteegh