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.

Template Questions /

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

2 different sortings of children


Go to End


2 Posts   2032 Views

Avatar
alialamshahi

Community Member, 5 Posts

24 August 2015 at 10:32am

Edited: 24/08/2015 10:34am

Hello everyone,

I have an events section in my website, every event is a child to this section, I want two different sorts, one sorted based on date, one sorted based on date > CURDATE() with interval of 7 days and if possible gets a sort of random as well (which would be something like current event) . this is what I did so far but doesnt work properly:

class:

<?php

class RadarHolder extends Page {
	
	
	private static $allowed_children = array (
		'RadarEntry'
	);
	
	
}

class RadarHolder_Controller extends Page_Controller {


public function SortedChildren(){

	$children = $this->Children();

	if( !$children )
		return null;

	$children->sort('Date');

	return $children;
}

public function CurrentEvent($count = 1){
		return RadarEntry::get()
					->sort('Date > CURDATE(), INTERVAL 7 DAY')
					->limit($count);

}

}

template:

<% if CurrentEvent %>
<% control CurrentEvent %>
...
<% end_control %>
<% end_if %>
.
.
<% loop SortedChildren %>
...
<% end_loop %>

What should I do to make it work?

Thanks in advance

Avatar
stallain

Community Member, 68 Posts

9 September 2015 at 3:05pm

Edited: 09/09/2015 3:06pm

Hi, I think what you're looking for is here : https://docs.silverstripe.org/en/3.1/developer_guides/model/data_model_and_orm/
You can try something like this (not tested) :

function SortedChildren() {
return $this->Children()->sort('Date', 'DESC');
}

function CurrentEvent(){
return RadarEntry::get()->filter(array('Date:GreaterThan'=> your limit, 'Date:LessThan'=> your limit))->sort('Date', 'ASC')->First();
}

function randomEvent() {
return RadarEntry::get()->sort(RAND())->First();
}