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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Sort field in SiteTree object


Go to End


4 Posts   1931 Views

Avatar
jaaf

Community Member, 24 Posts

17 February 2014 at 9:04pm

Edited: 17/02/2014 9:05pm

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

Community Member, 24 Posts

18 February 2014 at 12:29am

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

Forum Moderator, 5523 Posts

22 February 2014 at 9:25pm

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

Community Member, 24 Posts

23 February 2014 at 8:19am

Thank you Willr,

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