0
votes

The PaginationRecall component is giving me a headache, it let my application crash.

I have a region view, I can call it with /views/[number of region]

I found out that the reason for the crash is that it wants to go back to Page:5 although the url is changed to a different region which only has 1 or 2 pages. I updated to the latest cakePHP version [v2.3.5] as there was a mention about a fix for 'Preventing pagiation limit from overflowing the max integer value' But updating didn't solve my problem. Not sure if this is a problem with the PaginationRecal component or the Paginator itself.

For example; I can browse to the url ../regions/view/1/page:5 leave that page and when return back all fine [it remembered last page] But when changing the url to a different region ../regions/view/56/ it crashes with error: Error: The requested address '/regions/view/56' was not found on this server. Stack Trace

CORE/Cake/Controller/Controller.php line 1074

  • @deprecated Use PaginatorComponent instead */ public function paginate($object = null, $scope = array(), $whitelist = array()) { return $this->Components->load('Paginator', $this->paginate)->paginate($object, $scope, $whitelist); }

At first I didn't understand this crash untill I found out it was related to the page number. The problem never pops-up when browsing only on the first page. How can I fix this problem?

I have added the PaginatorRecall Component in my /app/Controller/AppController.php

 public $components = array('PaginationRecall');

This is the component: http://bakery.cakephp.org/articles/Zaphod/2012/03/27/paginationrecall_for_cakephp_2_x

Any help much appreciated.

2

2 Answers

0
votes

SOLVED

After investigating what the PaginatorRecall component is doing, I found out that it filters out only the page:number and stores it in a session.

I have added a rule to find out if the region is changed and if so then reset the page to page:1

Hope this helps someone else using this component as well.

0
votes

This is my solution to this problem and the bug that PaginationRecall have with the first page. Surely it is not the most optimal solution, but it is functional.

class PaginationRecallComponent extends Component {
    var $components = array('Session');
    var $Controller = null;

    function initialize(&$controller) {
        $this->Controller = & $controller;
        $options = array_merge($this->Controller->request->params, $this->Controller->params['url'], $this->Controller->passedArgs
        );
        $vars = array('page', 'sort', 'direction', 'filter');
        $keys = array_keys($options);
        $count = count($keys);
        for ($i = 0; $i < $count; $i++) {

            if (!in_array($keys[$i], $vars) || !is_string($keys[$i])) {

                unset($options[$keys[$i]]);
            }
        }
        //save the options into the session
        if ($options) {
            if ($this->Session->check("Pagination.{$this->Controller->modelClass}.options")) {
                $options = array_merge($this->Session->read("Pagination.{$this->Controller->modelClass}.options"), $options);
            }
            $this->Session->write("Pagination.{$this->Controller->modelClass}.options", $options);
            $this->Session->write("region",$this->Controller->modelClass.'/'.$this->Controller->view);
        }
        $region=$this->Session->read('region');
        //recall previous options       
        if ($this->Session->check("Pagination.{$this->Controller->modelClass}.options") && $region==$this->Controller->modelClass.'/'.$this->Controller->view ) {
            $options = $this->Session->read("Pagination.{$this->Controller->modelClass}.options");
            if(!isset($this->Controller->params['named' ]['page']) && isset($this->Controller->params['named' ]['direction']) )               
            {
                $options['page']=1;
            }
            $this->Controller->passedArgs = array_merge($this->Controller->passedArgs, $options);
            $this->Controller->request->params['named'] = $options;
        }
    }
}