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.

General Questions /

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

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

Pagination of dataobject error


Go to End
Reply


9 Posts   2663 Views

Avatar
potion_maker

Community Member, 36 Posts

5 December 2009 at 11:07am

Edited: 05/12/2009 7:43pm

Ok so I know there is tons of documentation and forum questions regarding pagination so I apologize if this has been addressed somewhere and I missed it. That being said here's the problem.

I have a data object and a custom search form for looking through it. The search seems to pull the correct results but when I click on my link to view the next page it pulls results up from all over the site.

here is the dataobject:

class Comedian extends DataObject {
   static $db = array (
      'FirstAppeared' => 'Date',
      'FirstName' => 'Varchar',
      'LastName' => 'Varchar',
      'Bio' => 'HTMLText',
      'Video' => 'HTMLText'
   );
   
   static $has_one = array(
      'Avatar' => 'Comedian_CustomImage',
   );
   
   static $default_sort = "LastName ASC";
   
   static $searchable_fields = array(
      'FirstName',
      'LastName'
   );
   
   static $search_heading = "LastName";
   static $search_content = "Firstname";
   
   static $summary_fields = array(
      'FirstName',
      'LastName',
    'Avatar.CMSThumbnail.Tag'
   );
   
   public function getCustomSearchContext() {
      $fields = $this->scaffoldSearchFields(array(
         'restrictFields' => array('LastName','FirstName')
      ));
      $filters = array(
         'LastName' => new PartialMatchFilter('LastName'),
         'FirstName' => new PartialMatchFilter('FirstName')
      );
      return new SearchContext(
         $this->class,
         $fields,
         $filters
      );
   }   
}

class Comedian_CustomImage extends Image {
   public function generateCustomImage($gd) {
      return $gd -> resizeByWidth(250);
   }
}

here is the controller:

class ComediansPage_Controller extends Page_Controller
{   
   public function ComedianSearchForm() {
      $context = singleton('Comedian')->getCustomSearchContext();
      $fields = $context->getSearchFields();
      $form = new Form($this, "SearchForm",
         $fields,
         new FieldSet(
            new FormAction('doComedianSearch', 'Find Comedian')
         )
      );
      return $form;
   }
   
   public function doComedianSearch($data, $form) {
   $context = singleton('Comedian')->getCustomSearchContext();
      $results = $this->getComedianResults($data);
      return $this->customise(array(
         'ComedianResults' => $results));
}

function getComedianResults($searchCriteria = array()) {
      if($searchCriteria != array()) {
         $start = ($this->request->getVar('start')) ? (int)$this->request->getVar('start') : 0;
         $limit = 4;
            
         $context = singleton('Comedian')->getCustomSearchContext();
         $query = $context->getQuery($searchCriteria, null, array('start'=>$start,'limit'=>$limit));
         $records = $context->getResults($searchCriteria, null, array('start'=>$start,'limit'=>$limit));
         if($records) {
            $records->setPageLimits($start, $limit, $query->unlimitedRowCount());
         }   
         return $records;
      }
   }
}

and finally the .ss template:

<% if ComedianResults.MoreThanOnePage %>
                  <div id="PageNumbers">
                     <p>
                        <% if ComedianResults.NotFirstPage %>
                           <a class="prev" href="$ComedianResults.PrevLink" title="View the previous page">Prev</a>
                        <% end_if %>
                     
                        <span>
                              <% control ComedianResults.PaginationSummary(4) %>
                              <% if CurrentBool %>
                                 $PageNum
                              <% else %>
                                 <% if Link %>
                                    <a href="$Link" title="View page number $PageNum">$PageNum</a>
                                 <% else %>
                                    &hellip;
                                 <% end_if %>
                              <% end_if %>
                           <% end_control %>
                        </span>
                     
                        <% if ComedianResults.NotLastPage %>
                           <a class="next" href="$ComedianResults.NextLink" title="View the next page">Next</a>
                        <% end_if %>
                     </p>
                  </div>
               <% end_if %>
            
               <% if ComedianResults %>
                  <p>Comedians:<br />
                     <% control ComedianResults %>
                     
                     $Avatar.SetWidth(100) <a style="font-style:smallCaps; font-size:16px;" href="comedians/display/$LastName/$FirstName">$FirstName $LastName</a>
                     <% end_control %>
                  </p>
               <% else %>
                  <p>Sorry, your search query did not return any results.</p>
               <% end_if %>

Avatar
dalesaurus

Community Member, 283 Posts

5 December 2009 at 1:17pm

The problem is you are not creating a paging link that keeps the query content in the URL

<% if Link %>
<a href="$Link" title="View page number $PageNum">$PageNum</a>
<% else %>
&hellip;
<% end_if %>

You are using $Link with is "http://adllc.net/comedy/comedians/display/&start=2" and not "http://adllc.net/comedy/comedians/display/S?start=2"

But as I refreshed it looks like you fixed it already?

Avatar
potion_maker

Community Member, 36 Posts

5 December 2009 at 1:55pm

dalesauras,
Thanks for the response but I think your a little confused as to what the problem is. There are two ways to browse through comedians. By letter (which is working fine - pagination and all) and by running a search query (working but pagination is messed up). If you run a search for the letter 's' (s because there are not many comedians in the db) and try to view page 2 of the search results you should see the error I'm getting.

Avatar
dalesaurus

Community Member, 283 Posts

5 December 2009 at 3:55pm

I see you are customizing the output with ComedianResults as both a control and within your search results return array. Is it possible that they are colliding and not getting the right data set?

Avatar
potion_maker

Community Member, 36 Posts

5 December 2009 at 4:46pm

I think you are on the right track but I'm not quite sure what you mean "as both a control and within your search results". Could you clarify what portion of the code you're talking about.

Avatar
sparkalow

Community Member, 8 Posts

5 December 2009 at 5:41pm

Edited: 05/12/2009 5:43pm

I'm on board with DaleSaurus I think pagination relies on the vars being in the query string? (start=2&limit=10 etc.) I solved this issue of pagination on search results by adding $form->setFormMethod('get'); to the search form and converting it to a get.

Avatar
potion_maker

Community Member, 36 Posts

5 December 2009 at 6:00pm

Edited: 05/12/2009 7:41pm

sparkalow your my hero. That worked like a charm. Cheers. The last problem I'm having with this is inserting a sort into the results. I tried adding:

$query = $context->getResults($searchCriteria, null, array('sort'=> 'LastName DESC','start'=>$start,'limit'=>$limit));

but to no avail.
also. .

ok found it. I feel dumb, the 'null' was the sort field. so context->getResults($searchCriteria, LastName ASC, array('start'=>$start,'limit'=>$limit));

Avatar
sparkalow

Community Member, 8 Posts

5 December 2009 at 7:21pm

Maybe try adding the public static $default_sort property to your dataobjec.

public static $default_sort = 'LastName ASC';

Go to Top