Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Sorting the dataobjects fetched

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

Page: 1
Go to End
Author Topic: 1284 Views
  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Sorting the dataobjects fetched Link to this post

    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.

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Sorting the dataobjects fetched Link to this post

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

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: Sorting the dataobjects fetched Link to this post

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

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Sorting the dataobjects fetched Link to this post

    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;
    }

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