Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » ModelAdmin with dates order by casted date in search results

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

Page: 1
Go to End
Author Topic: 732 Views
  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    ModelAdmin with dates order by casted date in search results Link to this post

    ModelAdmin managing the following model...

    class MyObject extends DataObject
    {
       static $db = array(
          'MyDate'          => 'Date',
       );
       public static $summary_fields = array (
          'MyDateText'   => 'DateFormatted'
       );
       static $casting = array(
          "MyDateText" => 'Text',
       );
       public function MyDateText(){return date('d/m/Y',DateManager::DateToPHPDate($this->MyDate));}
    }

    I've also done stuff to allow me to search, BUT the issue is that I now have lost the ability to click the column heading in the search results to order the data by that column... can anyone help me with this please?

    (I am using 2.4.0, but planning on getting to 2.4.3 in the new year, maybe I should wait?)

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: ModelAdmin with dates order by casted date in search results Link to this post

    Have a look at TableListField->isFieldSortable() - for performance reasons, we can't allow to sort on PHP getters, as we'd need to preload all records before sorting.

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: ModelAdmin with dates order by casted date in search results Link to this post

    Fair enough, I'll take another look at this in the new year when upgrading the latest version, and thank you for offering to help with date issues previously. At the moment it works, but I am sure I can achieve the same results with what feels like hacky code - and I hope to get this right too because I am only formatting the date with the casting the sort I was under the delusion it could be done but it makes sense that it cannot.

    732 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.