You can randomly sort the DataList before DataList over it, instead of randomly sorting the ArrayList :
public function AllTheKits($sort = '') { $kits = Versioned::get_by_stage('KitsPage', 'Live', '', $sort); $kitsArrayList = ArrayList::create(); foreach ($kits as $kit) { if ($kit->MemberID == Member::currentUserID()) { $kitsArrayList->push($kit); } } return $kitsArrayList; } public function getKitsRandom() { return $this->AllTheKits('RAND()')); }
As an additional note, you can filter the source DataList to retrieve the KitsPages that are related to this MemberID in the Versioned::get_by_stage :
public function AllTheKits($sort = '') { $kits = Versioned::get_by_stage( 'KitsPage', 'Live', 'MemberID = ' . Member::currentUserID(), $sort ); $kitsArrayList = ArrayList::create($kits); return $kitsArrayList; }
You can also just do this:
return KitsPage::get()->filter('MemberID', Member::currentUserID())->sort('RAND()');
When you browse the site live you only get live KitPages .
3dgoo source share