XF 2.0 ->whereIds(...) Use IDs array order

CMTV

Well-known member
Hi!

I have an ordered array of IDs gathered from complex SELECT query.

Now I need to get entities with IDs from my array but in the order they are listed in IDs array!

It can be done with this query:
SQL:
SELECT * FROM table WHERE id IN (5,4,3,1,6) ORDER BY FIELD(id, 5,4,3,1,6);

But the porblem is that I can't use FIELD(...) function in finder:
PHP:
$trophies = $this->finder('XF:Trophy')->whereIds($trophyIds)->("FIELD(`trophy_id`, " . implode($trophyIds, ',') . ")");
The code above throws an error:
error.webp
 
You're not calling any function in your posted code.

Code:
)->hereShouldBeAFunctionName("FIELD

Either way, $trophies is an ArrayCollection, so your should be able to sort it as you can sort an array.
 
Back
Top Bottom