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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Prev, next on dataobject page.


Go to End


10 Posts   4346 Views

Avatar
tomjohn

Community Member, 19 Posts

9 June 2015 at 9:23am

It's look simple, but I looked at many posts and I dont found working for me solution. I've got a page with portfolio. This is one page with realisations. Each single realisation subpage is a dataobject. On each one from this subpages I need two buttons: Next and Prev. How to do it?

Avatar
Pyromanik

Community Member, 419 Posts

9 June 2015 at 9:43am

Edited: 09/06/2015 9:46am

$prev=$this->Parent()->Children()->filter(['Sort'=>$this->Sort-1]);
//or
$next=SiteTree::get()->filter(['ParentID'=>$this->ParentID,'Sort'=>$this->Sort+1]);

Avatar
tomjohn

Community Member, 19 Posts

9 June 2015 at 10:00am

Thanks for your reply. Unfortunately I've got an error with this code: Uncaught Exception: Object->__call(): the method 'parent' does not exist on 'Portfolio'

This pages are not childrens. They are dataobjects like in this tutorial: http://docs.silverstripe.org/en/3.1/tutorials/extending_a_basic_site/

Avatar
Pyromanik

Community Member, 419 Posts

9 June 2015 at 10:45am

Edited: 09/06/2015 10:53am

In that case it's harder, without a defined sort.
I believe the default is creation date (which is bascially the ID).
So then

$this->WhateverTheHasOneToTheParentIsNamed()->WhateverTheHasManyToTheseRelatedPortfolioObjectsIsCalled()->filter('Created:GreaterThan',$this->Created)->first()

use the :LessThan filter for prev.

Or

Portfolio::get()->filter(['ParentRelationNameID'=>$this->ParentRelationNameID, 'Created:LessThan'=>$this->Created])->First()

Where the names of your relations are appropriate (I guess 'PortfolioHolder' maybe?)

Avatar
tomjohn

Community Member, 19 Posts

9 June 2015 at 11:06am

Summary page is PortfolioPage. I have errors with your code "unexpected ] ", so I changed it to:

public function PreviousPage() {    
Portfolio::get()->filter(array('PortfolioPageID'=>$this->PortfolioPageID, 'Created:LessThan'=>$this->Created))->First();

}
public function NextPage() {
Portfolio::get()->filter(array('PortfolioPageID'=>$this->PortfolioPageID, 'Created:GreaterThan'=>$this->Created))->First();
  
}

but I dont see nothing on site.

SS is

 <% loop $PreviousPage %>$Title<% end_loop  %>  <% loop $NextPage %>$Title<% end_loop  %>

Avatar
Pyromanik

Community Member, 419 Posts

9 June 2015 at 11:14am

Edited: 09/06/2015 11:16am

You need to return those.
return Portfolio::get()->...;

and you should use <% with $NextPage %>, not <% loop %> since you are retrieving a single object, not a list of objects.
Or alternatively $NextPage.Title (etc)

Avatar
tomjohn

Community Member, 19 Posts

9 June 2015 at 11:34am

You have right. Thank you for your help.

This almost work for me:

public function PreviousPage() {    
return  $this->PortfolioPage()->Portfolio()->filter('ID:LessThan',$this->ID)->first()  ;
}
//or
public function NextPage() { 
return  $this->PortfolioPage()->Portfolio()->filter('ID:GreaterThan',$this->ID)->first()  ;
}

Next work like charm, but previous show allway firts item with ID =1

Avatar
tomjohn

Community Member, 19 Posts

9 June 2015 at 11:41am

Edited: 09/06/2015 11:42am

Final working code:
PortfolioPage is page
Portfolio extends DataObject {$has_many)

php

public function PreviousPage() {    
return  $this->PortfolioPage()->Portfolio()->filter('ID:LessThan',$this->ID)->sort(array( 'ID' => 'DESC'))->first()  ;
}


public function NextPage() { 
return  $this->PortfolioPage()->Portfolio()->filter('ID:GreaterThan',$this->ID)->sort(array( 'ID' => 'ASC'))->first()  ;
}
 

ss

<% with $PreviousPage %>$Title <% end_with  %>  <% with $NextPage %>$Title <% end_with  %> 

I realy thank you once again. Without you I lost many hours.

Go to Top