Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Sorting and filtering children before pagination

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

Page: 1
Go to End
Author Topic: 1001 Views
  • kaanuni
    Avatar
    Community Member
    22 Posts

    Sorting and filtering children before pagination Link to this post

    Hi,

    This is all for SS3

    I have defined the following function in my controller:

    public function PaginatedChildren() {
    $paginatedList = new PaginatedList($this->Children(), $this->request);
    if( $this->NChildren == 0 ) $nChildren = 10;
    else $nChildren = $this->NChildren;
    $paginatedList->setPageLength( $nChildren );
    return $paginatedList;
    }

    I would like to be able to sort and filter the output of Children() before wrapping it in a PaginatedList and sending it to the template.
    I have checked the API documentation to find the Children function, but the closest thing i found was the ChildrenOf method in ContentController. So I am curious where Children() is documented. Is it just an alias to ChildrenOf? If so where are these aliases defined.

    These questions are more a matter of curiosity than anything, but I would really like to know how to sort and filter the output of $this->Children() in the controller. ChildrenOf() returns an SS_List, so I assume Children() returns one too. But SS_List doesn't have any sorting or filtering options and the documentation even goes far as to say that add doesn't guarantee a position in the list.

    Given this what is the best way to proceed?
    Should I be using custom SQL, maybe DataList?
    Should I wrap the SS_List with SS_ListDecorator which appears to have exactly the functionality I need (although it appears to be completely undocumented)
    Or is there another way to do this?

    I am leaning more towards DataList as I guess this would pass off the filtering and sorting to the database server, which I suppose would be preferable. On the other hand SS_ListDecorator looks so much easier to implement (if only it was documented).

    Also what is the preferred way of accessing $_GET parameters in the controller?

    Thank You.

  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: Sorting and filtering children before pagination Link to this post

    Hi kaanuni,

    Hierarchy.php #434: public function Children()

    Calls Hierarchy.php #560: public function stageChildren($showAll = false) Note that you can extend this function using augmentStageChildren.

    Here you can at least see the query that returns the children. A similar function SortedChildren($sort) (in your Page class even) could could return sorted pages...

    Martine

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Sorting and filtering children before pagination Link to this post

    $this->Children() returns a type of List, you should be able to use $this->Children()->filter(...)->sort(..)

  • kaanuni
    Avatar
    Community Member
    22 Posts

    Re: Sorting and filtering children before pagination Link to this post

    I tried modifying to code to

    public function PaginatedChildren() {
    $paginatedList = new PaginatedList($this->Children()->sort('Title'), $this->request);
    if( $this->NChildren == 0 ) $nChildren = 10;
    else $nChildren = $this->NChildren;
    $paginatedList->setPageLength( $nChildren );
    return $paginatedList;
    }

    and

    public function PaginatedChildren() {
    $paginatedList = new PaginatedList($this->Children(), $this->request);
    if( $this->NChildren == 0 ) $nChildren = 10;
    else $nChildren = $this->NChildren;
    $paginatedList->setPageLength( $nChildren );
    return $paginatedList->sort('Title');
    }

    With the second version pagination is lost.

    With both versions a page with about 160 children returns:

    Server error
    Sorry, there was a problem with handling your request.

    Using the simple theme.

    Another page with 16 children also does this. All the others which have between 2 and 13 children work fine with the first version.

    There is another odd problem with the 16 child page. The unsorted version refuses to display the second page (start=10), showing the same error. I tried reducing the start value and 4 is the first problematic one. I'm going to have to turn on debug mode for this problem.

  • kaanuni
    Avatar
    Community Member
    22 Posts

    Re: Sorting and filtering children before pagination Link to this post

    Ok so with the help of dev mode i fixed the problem with the 16 child page. It had to do with a completely different part of the code. Basically a cms user did not enter some data, and i forgot to check to see if data was entered. Oddly enough, this also solved the problem with the 160 child page.

    The first method works fine. I'm going to do a benchmark to see how much slower the page is with the current sorting method.

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