doctrine2 order by array with ids
Asked Answered
N

5

8

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

Nilgai answered 23/5, 2014 at 16:41 Comment(1)
Questions should never contain solutions -- that is what answers are for.Tomato
N
0

I found the solution, it is possible using FIELD mysql extension with https://github.com/beberlei/DoctrineExtensions

Thanks

Marc

Nilgai answered 11/2, 2016 at 17:5 Comment(1)
Here is an example of how to register this DQL function in Symfony and how to use it, just in case that you are using MySQL as well: ourcodeworld.com/articles/read/1162/…Landgravine
E
8

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];
 });
Engraft answered 7/5, 2015 at 22:56 Comment(0)
P
0

If you are using MySQL, sorting with the FIELD() function could accomplish this. DQL doesn't have built-in support for this function, so you'll either have to create a user defined function (see this answer), or create a native query.

Prissy answered 23/5, 2014 at 18:6 Comment(0)
E
0

I have a solution that is probably very inefficient, but it works. Make sure your results are ordered by id.

$users = $entityManager
    ->getRepository('User')
    ->findById($userIds, ['id' => 'ASC']);

$userIdsCopy = $userIds;
sort($userIdsCopy);
array_multisort($userIds, $userIdsCopy);
array_multisort($userIdsCopy, $users);

It's hard to explain how this works, but basically you "remember" the operations that happen when you sort the userId-array, then you apply the opposite of that to the query result.

Engraft answered 7/5, 2015 at 22:45 Comment(0)
N
0

I found the solution, it is possible using FIELD mysql extension with https://github.com/beberlei/DoctrineExtensions

Thanks

Marc

Nilgai answered 11/2, 2016 at 17:5 Comment(1)
Here is an example of how to register this DQL function in Symfony and how to use it, just in case that you are using MySQL as well: ourcodeworld.com/articles/read/1162/…Landgravine
Z
-1

Which id do you want to order by?

You can use...

->orderBy('u.id', 'asc')
// or n.id
// or c.id

Or you can use multiple order by's ("by's" doesn't seem right to me)...

->addOrderBy('u.id', 'asc')
->addOrderBy('u.name', 'desc') // if it exists
Zacharia answered 25/5, 2014 at 2:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.