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.

Archive /

Our old forums are still available as a read-only archive.

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

Problem with dataObjectSet and setPageLimits


Go to End


3 Posts   3428 Views

Avatar
Internet Marketing Services GmbH

Community Member, 6 Posts

5 April 2008 at 9:40am

Hi,

i'm currently working on a webservice module which sends a request to a branchbook service. Fetching the branchbook entries works fine, all are stored in one array.

To display the entries i use a dataObjectSet with following code:

$output = new DataObjectSet();
$output->setPageLimits((intval($_GET['start']) ? $_GET['start'] : 0), 5, sizeof($entries));

foreach($entries as $entry) {
	$output->push(new ArrayData($entry));
}

return $output;

and in the template

<% if Entries.MoreThanOnePage %>
	<div id="PageNumbers">
		<% if Entries.NotLastPage %>
			<a class="next" href="$Entries.NextLink" title="View the next page">Next</a>
		<% end_if %>
		<% if Entries.NotFirstPage %>
			<a class="prev" href="$Entries.PrevLink" title="View the previous page">Prev</a>
		<% end_if %>
		<span>
			<% control Entries.Pages %>
				<% if CurrentBool %>
					$PageNum
				<% else %>
					<a href="$Link" title="View page number $PageNum">$PageNum</a>
				<% end_if %>
			<% end_control %>
		</span>
		<p>Page $Entries.CurrentPage of $Entries.TotalPages</p>
	</div>
<% end_if %>

<% control Entries %>
	<h2>$customer</h2>
<% end_control %>

Although i've set to display only 5 entries per page all entries are displayed.

Pagination at the beginning of the template works fine. The list of available pages updates correctly when setting $_GET['start'] (active page, prev and next link).

How could i tell the dataObjectSet only to display the requested number of entries per page. Is it necessary to do that manually in the foreach loop?

Hope somebody could help me and my dataObjectSet :-)

Good Night (in Germany it's time to go to bed now)

Avatar
Ingo

Forum Moderator, 801 Posts

6 April 2008 at 7:07pm

hey christian,

DataObjectSet->setPageLimits() is just a helper for pagination which sets internal counters - meaning if you fill your set with 20 items, it will still iterate over all 20. the key is to put only 5 objects into your $entries set, but let it know through setPageLimits() that actually there's 20.

// bad performance, see SQLQuery->unlimitedRowCount() for faster implementation
$allEntries = DataObject::get('Entry');
$start = (isset($_GET['start'])) ? (int)$_GET['start'] : 0;
$length = 5;
$pagedEntries = DataObject::get('Entry', '', '', '', "{$start},{$length}");
pagedEntries->setPageLimits($start, $length, $allEntries->Count());

Avatar
Internet Marketing Services GmbH

Community Member, 6 Posts

6 April 2008 at 9:51pm

Hi Ingo,

thanks for your help.