Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Problem with dataObjectSet and setPageLimits

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

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

Page: 1
Go to End
Author Topic: 2571 Views
  • christian
    Avatar
    Community Member
    6 Posts

    Problem with dataObjectSet and setPageLimits Link to this post

    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)

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Problem with dataObjectSet and setPageLimits Link to this post

    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());

  • christian
    Avatar
    Community Member
    6 Posts

    Re: Problem with dataObjectSet and setPageLimits Link to this post

    Hi Ingo,

    thanks for your help.

    2571 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.