Cakephp and paginating multiple result sets

前端 未结 2 1699
后悔当初
后悔当初 2021-01-15 00:30

I have pagination working fine for when $this->paginate is called once in an action, but it does not appear to be very good support for multiple calls. Here is what I\'m loo

2条回答
  •  深忆病人
    2021-01-15 00:57

    I had the same problem and solved the problem with the following Classes in cakephp version 2.5.3 I have not checked whether my approach also works in older versions. First, I created a Helper and Component class which extends PaginationComponent and PaginationHelper and overwrote some Methods.

    /app/Controller/Component/_PaginatorComponent.php

    _getObject($object);
    
            $options = $this->mergeOptions($object->alias); //get options
            $params = $this->Controller->request->params; //get params
    
            //if custom page name is set
            if(isset($params['named'][$options['pageRequestName']]))
            {
                //get the page number and set it as 'page' in the request (for other methods in the Component)
                $page = $params['named'][$options['pageRequestName']];
                $this->Controller->request->params['named']['page'] =intval($page);
            }
            else
                $this->Controller->request->params['named']['page'] = 1; //else set the page number to 1
    
            $pageRequestName = null; //important for parent::paginate
    
            $results = parent::paginate($object, $scope, $whitelist); // TODO: Change the autogenerated stub
            unset($this->Controller->request->params['named']['page']); //remove the appended page number
    
            //get the options again
            $options = $this->mergeOptions($object->alias);
            //and add the pageRequestName to the paging array
            if(isset($options['pageRequestName']))
            $this->Controller->request['paging'] = array_merge_recursive(
                (array)$this->Controller->request['paging'],
                array($object->alias => array('pageRequestName'=>$options['pageRequestName']))
            );
            return $results;
        }
    
    }
    

    /app/View/Helper/_PaginatorHelper.php

    params($this->model);
                if(isset($params['pageRequestName']))
                {
                    $url[$params['pageRequestName']] = $url['page'];
                    unset($url['page']);
                }
            }
            return parent::link($title,$url,$options);
        }
    
        protected function _pagingLink($which, $title = null, $options = array(), $disabledTitle = null, $disabledOptions = array())
        {
            //Prevent the deletion of the variable model
            $disabledOptions['model'] = $options['model'];
            return parent::_pagingLink($which, $title, $options, $disabledTitle, $disabledOptions); // TODO: Change the autogenerated stub
        }
    
        private $model = null;
        public function numbers($options = array())
        {
            //Only to set the model again
            if(isset($options['model']))
                $this->model = $options['model'];
            return parent::numbers($options); // TODO: Change the autogenerated stub
        }
    
    
    }
    

    And a View Element to use the Pagination quickly:

    /app/View/Element/paging.ctp

    $modelName); echo $this->Paginator->prev('< ' . __('zurück'), $options, null, array('class' => 'prev disabled')); echo $this->Paginator->numbers(array('separator' => '')+$options); echo $this->Paginator->next(__('vor') . ' >', $options, null, array('class' => 'next disabled')); ?>

    You can call the paging-element in your view like this: (Documentation)

    element('paging',array('modelName'=>'Customer'));
    ?>
    

    If you want use the new created paginator, you have to say cakephp to use your class instead of the normal paginator. In your controller, you set the PaginatorHelper and PaginatorComponent like this: (found here)

    public $helpers = array('Paginator' => array('className' => '_Paginator' ));
    public $components = array('Paginator' => array('className' => '_Paginator' ));
    

    The usage inside your controller. The $paginate have to be grouped into the Modelgroups: (Documentation)

    public $paginate = array(
            'Customer' => array (
                'limit' => 25,
                'pageRequestName' => 'cpage'
            ),
            'Employer' => array (
                'limit' => 25,
                'pageRequestName' => 'erpage'
            ),
            'Employee' => array (
                'limit' => 25,
                'pageRequestName' => 'eepage'
            )
        );
    

    But I use it in the following context:

    $this->Paginator->settings['Customer'] = array(
                'conditions'=>array(
                    'OR'=>array(
                        array('name LIKE'=>'%'.$search_query.'%'),
                    )
                ),
                'limit' => 5,
                'pageRequestName' => 'cpage'
            );
            $result = $this->Paginator->paginate('Customer');
    

    I hope I could help some people who do not want a javascript hack, but a real possibility to have several Paginations on one side.

    At the moment you can only use 'paramType' => 'named'

    In my opinion, the original code in PaginationComponent and PaginationHelper are horrible to read, extend and adapt.

提交回复
热议问题