Faloude Faloude - 1 month ago 9
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)

Foo::get()->byIDs($key_list);


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

How to maintain the same order?

Answer

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);
   $aFooList->push($oFoo); 
}
return $oFoo;
Comments