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.

General Questions

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

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

Sort existing values over nulls


Reply

4 Posts   339 Views

Avatar
svandragt

16 September 2013 at 8:43pm (Last edited: 16 September 2013 9:23pm), Community Member, 44 Posts

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

18 September 2013 at 2:01am Community Member, 94 Posts

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

18 September 2013 at 4:02am Community Member, 215 Posts

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

Avatar
svandragt

18 September 2013 at 7:37pm (Last edited: 18 September 2013 7:41pm), Community Member, 44 Posts

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.