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

Sorting the dataobjects fetched


Go to End


4 Posts   2108 Views

Avatar
Ryan M.

Community Member, 309 Posts

17 February 2011 at 3:27pm

So I know you can change the sorting in DataObject::get but I'm wondering if it's possible to accomplish the same result from the template without having to build a function in the controller that fetches the objects via DataObject::get with sorting parameters defined.

I'm just mainly looking to eliminate that extra step. The default behavior is to list the objects with oldest first, newest last, and that needs to be reversed for a particular instance.

Avatar
swaiba

Forum Moderator, 1899 Posts

18 February 2011 at 12:53am

something like this...
http://www.silverstripe.org/general-questions/show/15690?start=8#post298878
(with an extra param for sort)
?

Avatar
Ryan M.

Community Member, 309 Posts

18 February 2011 at 7:43am

Well.. not really, that's pretty much the same thing. Like I said, I was just mainly looking to eliminate the extra step of having to build a function somewhere for this.

You know how images can be resized using $Image.SetHeight, .SetWidth, etc? Something like that, like $Object.Sort(created, desc).

Either way, looks like the only way is to do it like the example you showed me or this:

function CustomObject() {
return DataObject::get('CustomObject', '', 'Created DESC');
}

Avatar
swaiba

Forum Moderator, 1899 Posts

18 February 2011 at 12:00pm

fair enough - I thought the post would be extremely flexible - you only one function, that you could then call from the template like this... In fact, I'm going to test it in my code for simple cases.

<% control GetStuff(MyDataObject,Description LIKE '%test%',Name ASC) %>

How about this, with php;s late binding maybe the following would work?

<% control Sort(MethodName,FieldName,ASC) %>

//--

function Sort($strMethodName,$strSortFeild,$strSortDirection) {
   $dos = $this->$strMethodName;
   $dos->sort($strSortFeild,$strSortDirection);
   return $dos;
}