How to sort DataObject by rand() in 5.1

Hey @LanceH

I believe this was changed in 5.1 with a dedicated function for it.

Try this (not tested):

Dataobject::get()
   ->filter(['CategoryID' => 1, 'TypeID' => 1])
   ->limit(3)
   ->shuffle();  // shuffle does the random sorting

Edit
There used to be a note in the source which mentions sort() doesn’t support raw SQL (updated September '23), which is effectively what what rand() was. But shuffle() seems to be the suggested way to do this now.

1 Like