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.

Data Model Questions

Sorting the dataobjects fetched


4 Posts   1329 Views

Ryan M.

17 February 2011 at 3:27pm Community Member, 309 Posts

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.


18 February 2011 at 12:53am Forum Moderator, 1796 Posts

something like this...
(with an extra param for sort)

Ryan M.

18 February 2011 at 7:43am Community Member, 309 Posts

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');


18 February 2011 at 12:00pm Forum Moderator, 1796 Posts

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;
return $dos;