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

Sort field in SiteTree object


Reply

4 Posts   433 Views

Avatar
jaaf

17 February 2014 at 9:04pm (Last edited: 17 February 2014 9:05pm), Community Member, 24 Posts

Hi,

I would like to use the method described in a thread on the forums
at http://www.silverstripe.org/customising-the-cms/show/6320?start=8#ForumContent
Here is a partial reproduction of the conclusive post in this thread:

cbolt's solution worked well for me. I made two minor edits...

PreviousPage function would fail if Children return a dataobjectset with no children. So updated the function to use Count():

function PreviousPage() {
$where = "ParentID = {$this->ParentID} AND Sort < {$this->Sort}";
$pages = DataObject::get("SiteTree", $where, "Sort DESC", "", 1);
if($pages) {
foreach($pages as $page) {
// if page has a child go to the last child page
$children = $page->AllChildren();
if ($children->Count()) {
foreach ($children as $child) {
continue;
}
return $child;
}
return $page;
}
}

Though being a beginner with SilverStripe and a rather non experienced programmer in php/msyql , my general rule of conduct is trying to understand what I am applying when coming from other more experienced people.

The trouble I have here is in understanding what the Sort field in the SiteTree database columns means. I can see, having a look at this field in the database that it may have various numerical value.
Where can I find an explanation of how to interpret these values ?
Many thanks in advance:

Avatar
jaaf

18 February 2014 at 12:29am Community Member, 24 Posts

Hi.
I have not found any explanation but it seems that the value of the Sort fields is the order in which a SiteTree appears for a given level of hierarchy
It restarts at 1 at each level in the tree for SiteTrees that have a common parent.
I hope I am right.

Avatar
Willr

22 February 2014 at 9:25pm Forum Moderator, 5511 Posts

Yes the sort value is level specific. It is generated automatically by SilverStripe when you create a page and altered whenever you reorder the pages in the sitetree.

From your code sample it sounds like it's trying to get the previous page. The previous page is the one which has the next lowest sort after the current page (say 1, 3, 6, 8 and you're on page 6 the previous page would be 3).

Avatar
jaaf

23 February 2014 at 8:19am Community Member, 24 Posts

Thank you Willr,

That's clear to me now.
I go on enjoying SilverStripe.