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 and filtering children before pagination


Reply


5 Posts   1336 Views

Avatar
kaanuni

Community Member, 22 Posts

24 October 2012 at 4:14am

Edited: 24/10/2012 4:15am

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.

Avatar
martimiz

Forum Moderator, 1105 Posts

24 October 2012 at 5:17am

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

Avatar
Willr

Forum Moderator, 5513 Posts

24 October 2012 at 8:14pm

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

Avatar
kaanuni

Community Member, 22 Posts

1 November 2012 at 10:46pm

Edited: 01/11/2012 10:46pm

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.

Avatar
kaanuni

Community Member, 22 Posts

1 November 2012 at 11:02pm

Edited: 01/11/2012 11:23pm

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.