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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Sort existing values over nulls


Go to End


4 Posts   801 Views

Avatar
svandragt

Community Member, 44 Posts

16 September 2013 at 8:43pm

Edited: 16/09/2013 9:23pm

I have a Task model with a DueDate SS_Datetime field. To see the 'soonest due' tasks first, I sort my tasks by DueDate ASC.. This works as expected, apart from the fact that tasks without due dates are shown before tasks with due dates.

I was wondering if there was a custom sort; or a field modifier like sort('DueDate:exists') etc?

Alternatively, I can create a HasDueDate Boolen field that's set to !empty($this->DueDate) onBeforeWrite, but maybe there is a better way?

Avatar
Bereusei

Community Member, 96 Posts

18 September 2013 at 2:01am

Maybay this is a little bit nicer:

In ModelAdmin:

public function getList(){
		$list = parent::getList();
		$list->exclude('DueDate', ' '); //or try: $list->exclude('DueDate', null); 
                return $list;
}

With these function you exclude all empty DueDates from listview.

Avatar
Devlin

Community Member, 344 Posts

18 September 2013 at 4:02am

$list->where('DueDate IS NOT NULL');

Avatar
svandragt

Community Member, 44 Posts

18 September 2013 at 7:37pm

Edited: 18/09/2013 7:41pm

Your suggestions are much appreciated guys, but I'm looking for a SORT not a filter :) (sort null before asc).

So I will probably do 2 data queries, one for null due dates and one for tasks that have them.