Jump to:

22975 Posts in 11620 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Pagination of dataobject error

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

Page: 1 2
Go to End
Author Topic: 2370 Views
  • potion_maker
    Avatar
    Community Member
    36 Posts

    Pagination of dataobject error Link to this post

    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 %>

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Pagination of dataobject error Link to this post

    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?

  • potion_maker
    Avatar
    Community Member
    36 Posts

    Re: Pagination of dataobject error Link to this post

    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.

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Pagination of dataobject error Link to this post

    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?

  • potion_maker
    Avatar
    Community Member
    36 Posts

    Re: Pagination of dataobject error Link to this post

    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.

  • sparkalow
    Avatar
    Community Member
    8 Posts

    Re: Pagination of dataobject error Link to this post

    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.

  • potion_maker
    Avatar
    Community Member
    36 Posts

    Re: Pagination of dataobject error Link to this post

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

  • sparkalow
    Avatar
    Community Member
    8 Posts

    Re: Pagination of dataobject error Link to this post

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

    public static $default_sort = 'LastName ASC';

    2370 Views
Page: 1 2
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.