Jump to:

23472 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Sort existing values over nulls

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

Page: 1
Go to End
Author Topic: 326 Views
  • svandragt
    Avatar
    Community Member
    44 Posts

    Sort existing values over nulls Link to this post

    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?

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Sort existing values over nulls Link to this post

    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.

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Sort existing values over nulls Link to this post

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

  • svandragt
    Avatar
    Community Member
    44 Posts

    Re: Sort existing values over nulls Link to this post

    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.

    326 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.