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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Help with pagination in DataObject


Reply


3 Posts   706 Views

Avatar
Mauro74

Community Member, 30 Posts

4 November 2011 at 12:04am

Hi all,

I'm pretty new to Silverstripe and PHP in general, so I'm learning by tutorials. In the well known website ssbits.com I found a very useful tutorial called 'DataObject as pages' ([url]http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-1-keeping-it-simple/[/url]). The tutorial is great, it shows how to create a list of staff members without using pages and using the DataObjectManager in the CMS.

I used the tutorial to implement a list of events, but now I'm stuck finding a way to paginate the list! Anyone could help me please?
here's my controller:

class GigPage_Controller extends Page_Controller
{
//Allow our 'show' function as a URL action
static $allowed_actions = array('show');

//Get the current gig detail from the URL, if any
public function getGigDetail()
{
$Params = $this->getURLParams();

if(is_numeric($Params['ID']) && $GigDetail = DataObject::get_by_id('GigDetail', (int)$Params['ID']))
{
return $GigDetail;
}
}

//Displays the gig detail detail page, using GigPage_show.ss template
function show()
{
if($GigDetail = $this->getGigDetail())
{
$Data = array(
'GigDetail' => $GigDetail
);

//return our $Data array to use on the page
return $this->Customise($Data);
}
else
{
//Gig not found
return $this->httpError(404, 'Sorry that member of staff could not be found');
}
}

//Return our custom breadcrumbs
public function Breadcrumbs() {

//Get the default breadcrumbs
$Breadcrumbs = parent::Breadcrumbs();

if($GigDetail = $this->getGigDetail())
{
//Explode them into their individual parts
$Parts = explode(SiteTree::$breadcrumbs_delimiter, $Breadcrumbs);

//Count the parts
$NumOfParts = count($Parts);

//Change the last item to a link instead of just text
$Parts[$NumOfParts-1] = ('<a href="' . $this->Link() . '">' . $Parts[$NumOfParts-1] . '</a>');

//Add our extra piece on the end
$Parts[$NumOfParts] = $GigDetail->Place .' - '. $GigDetail->Date;

//Return the imploded array
$Breadcrumbs = implode(SiteTree::$breadcrumbs_delimiter, $Parts);
}

return $Breadcrumbs;
}

}

here's my template:

<div class="typography">
<% if GigDetails %>
<div id="gig-list">
<% control GigDetails %>
<div class="gig">
<h3>$Place</h3>
<p>$Date.Nice - on stage at $Time</p>
<p>Address: $Address</p>
<p><a href="$Link">see map &raquo;</a></p>
</div>
<% end_control %>
</div>
<% end_if %>
</div>

Thanks in advance!

Mauro

Avatar
UncleCheese

Forum Moderator, 4096 Posts

4 November 2011 at 3:16am

I don't see anything in your code that returns a DataObjectSet. On your controller, you have <% control GigDetails %>, which I assume returns a DOS, but that function isn't anywhere in your code.

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
Mauro74

Community Member, 30 Posts

4 November 2011 at 3:53am

Edited: 04/11/2011 5:10am

Hi UncleCheese, you're right I cannot see the function that return a DOS neither, so I'm posting the entire code here so you can have a better understanding of what's going on, because I don't! :)

GigPage.php:

class GigPage extends Page
{
static $has_many = array(
'GigDetails' => 'GigDetail'
);

public function getCMSFields()
{
$fields = parent::getCMSFields();

$manager = new DataObjectManager(
$this,
'GigDetails',
'GigDetail'
);
$fields->addFieldToTab("Root.Content.GigDetails", $manager);

return $fields;
}
}

class GigPage_Controller extends Page_Controller
{
//Allow our 'show' function as a URL action
static $allowed_actions = array('show');

//Get the current gig detail from the URL, if any
public function getGigDetail()
{
$Params = $this->getURLParams();

if(is_numeric($Params['ID']) && $GigDetail = DataObject::get_by_id('GigDetail', (int)$Params['ID']))
{
return $GigDetail;
}
}

//Displays the gig detail detail page, using GigPage_show.ss template
function show()
{
if($GigDetail = $this->getGigDetail())
{
$Data = array(
'GigDetail' => $GigDetail
);

//return our $Data array to use on the page
return $this->Customise($Data);
}
else
{
//Gig not found
return $this->httpError(404, 'Sorry that member of staff could not be found');
}
}

//Return our custom breadcrumbs
public function Breadcrumbs() {

//Get the default breadcrumbs
$Breadcrumbs = parent::Breadcrumbs();

if($GigDetail = $this->getGigDetail())
{
//Explode them into their individual parts
$Parts = explode(SiteTree::$breadcrumbs_delimiter, $Breadcrumbs);

//Count the parts
$NumOfParts = count($Parts);

//Change the last item to a link instead of just text
$Parts[$NumOfParts-1] = ('<a href="' . $this->Link() . '">' . $Parts[$NumOfParts-1] . '</a>');

//Add our extra piece on the end
$Parts[$NumOfParts] = $GigDetail->Place .' - '. $GigDetail->Date;

//Return the imploded array
$Breadcrumbs = implode(SiteTree::$breadcrumbs_delimiter, $Parts);
}

return $Breadcrumbs;
}

}

GigDetail.php:

class GigDetail extends DataObject
{
//db fields
static $db = array (
'Place' => 'Varchar(255)',
'Date' => 'Date',
'Time' => 'Varchar(50)',
'Address' => 'Varchar(255)',
//'Address' => 'HTMLText', to return HTML content
'Postcode' => 'Varchar(50)',
);

//Relations
static $has_one = array (
'GigPage' => 'GigPage',
);

//Fields to show in the DOM table
static $summary_fields = array(
'Place' => 'Place',
'Date' => 'Date',
'Time' => 'Time',
'Address' => 'Address'
);

//Fields for the DOM Popup
public function getCMSFields()
{
return new FieldSet(
new TextField('Place'),
new DatePickerField('Date','Gig Date (for example: 20/12/2010)'),
new TextField('Time', 'Time (for example: 20.30)'),
new TextareaField('Address', 'Address'),
//new SimpleTinyMCEField('Address', 'Address'), to use HTML editor
new TextField('Postcode', 'Postcode')
);
}

//Return the link to view this gig detail
public function Link()
{
if($GigPage = $this->GigPage())
{
$Action = 'show/' . $this->ID;
return $GigPage->Link($Action);
}
}
}

GigPage.ss:

<div class="typography">
<% if GigDetails %>
<div id="gig-list">
<% control GigDetails %>
<div class="gig">
<h3>$Place</h3>
<p>$Date.Nice - on stage at $Time</p>
<p>Address: $Address</p>
<p><a href="$Link">see map &raquo;</a></p>
</div>
<% end_control %>
</div>
<% end_if %>
</div>

GigPage_show.ss

<div class="breadcrumbs">
<p>$Breadcrumbs</p>
</div>
<div class="typography">
<h2>Gig Detail</h2>
<% control GigDetail %>
<h3>$Place</h3>
<p>$Date.Nice - on stage at $Time</p>
<p>Address: $Address</p>
</div>
<% end_control %>
</div>

Thanks very much for your help!
Mauro