Faloude Faloude - 1 year ago 103
SQL Question

SilverStripe 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 do we maintain the same order when retrieving these items?

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download