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.

Data Model Questions /

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

ModelAdmin with dates order by casted date in search results


Go to End


3 Posts   1332 Views

Avatar
swaiba

Forum Moderator, 1899 Posts

3 December 2010 at 12:12am

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?)

Avatar
Ingo

Forum Moderator, 801 Posts

15 December 2010 at 9:57pm

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.

Avatar
swaiba

Forum Moderator, 1899 Posts

16 December 2010 at 8:32am

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.