Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Object sort by RAND() doesn't work


Go to End


1365 Views

Avatar
lulu

Community Member, 7 Posts

23 July 2016 at 2:20am

Hi,

I've just updated my SS from 3.1 to 3.4 version and I have one small issue - sort function doesn't work anymore but not completelty.

Here is bit of my code:

public function RelatedDownloads() {

	$Related = new ArrayList;	
	
	// take two items from download section
	$DownloadPage = DownloadPage::get()->sort('RAND()')->limit(2);
	
	// take one element from resource section from specific category
	$RelatedResources = Resource::get();
	$RelatedResources = $RelatedResources->leftJoin('Resource_Categories', "Resource.ID = Resource_Categories.ResourceID")->filter('Resource_Categories.ResourceCategoryID', array('5','6'))->sort('RAND()')->limit(1);
	
	// merge downloads and resource together
	foreach( $DownloadPage as $obj ) $Related->push( $obj );
	foreach( $RelatedResources as $obj ) $Related->push( $obj );
	
	// reorder them randomly
	$Related = $Related->sort('_SortColumn0', 'RAND()');

	return $Related;	
	
}

That was working fine on 3.1 now I'm getting error:

[22-Jul-2016 14:41:05] Error at \framework\model\ArrayList.php line 378: Uncaught InvalidArgumentException: Invalid sort() direction (http://www.domain.com/documents/?download=10995)

However when I change RAND() to ASC or DESC it works fine...

Any clues?

Many thanks, Alessia.