Faloude Faloude - 1 year ago 60
SQL Question

SilverStipe get by id in exact order

Suppose you have an array of keys

$key_list = array(3, 6, 2);

And you want to retrieve records from a certain table, using these keys as identifiers (WHERE ID = id_from_key_list)


This returns the rows with the ID's that match those in
(3, 6 and 2) but not in that order.

How to maintain the same order?

Answer Source

What you might need to do is to run a foreah loop of the IDs and push each Foo Object into an ArrayList

$aFooList = ArrayList::create(); 
foreach ($key_list as $key_list_id){
   $oFoo = Foo::get()->byID($key_list_id);
return $oFoo;