我有同样的问题并解决了cakephp 版本 2.5.3中的以下类的问题
我没有检查我的方法是否也适用于旧版本。首先,我创建了一个 Helper 和 Component 类,它扩展了 PaginationComponent 和 PaginationHelper 并覆盖了一些方法。
/app/Controller/Component/_PaginatorComponent.php
<?php
/**
* Filename: _PaginatorComponent.php
* User:
* Date: 18.01.14
*/
App::import('Component', 'Paginator');
class _PaginatorComponent extends PaginatorComponent{
public function paginate($object = null, $scope = array(), $whitelist = array())
{
//Adopt from the beginning of parent::paginate
if (is_array($object))
$object = null;
$object = $this->_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
<?php
/**
* Filename: _PaginatorHelper.php
* User:
* Date: 18.01.14
*/
App::uses('PaginatorHelper', 'View/Helper');
class _PaginatorHelper extends PaginatorHelper{
public function link($title, $url = array(), $options = array()) {
//change the page parameter 'page' to the custom pageRequestName
if(isset($url['page']))
{
$params = $this->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
}
}
和一个快速使用分页的视图元素:
/app/View/Element/paging.ctp
<div class="paging">
<?php
$options = !isset($modelName)?array():array('model'=>$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'));
?>
</div>
您可以像这样在视图中调用分页元素:(文档)
<?php
echo $this->element('paging',array('modelName'=>'Customer'));
?>
如果你想使用新创建的分页器,你必须说 cakephp 来使用你的类而不是普通的分页器。在您的控制器中,您像这样设置 PaginatorHelper 和 PaginatorComponent:(在此处找到)
public $helpers = array('Paginator' => array('className' => '_Paginator' ));
public $components = array('Paginator' => array('className' => '_Paginator' ));
控制器内部的用法。$paginate 必须被分组到模型组中:(文档)
public $paginate = array(
'Customer' => array (
'limit' => 25,
'pageRequestName' => 'cpage'
),
'Employer' => array (
'limit' => 25,
'pageRequestName' => 'erpage'
),
'Employee' => array (
'limit' => 25,
'pageRequestName' => 'eepage'
)
);
但我在以下情况下使用它:
$this->Paginator->settings['Customer'] = array(
'conditions'=>array(
'OR'=>array(
array('name LIKE'=>'%'.$search_query.'%'),
)
),
'limit' => 5,
'pageRequestName' => 'cpage'
);
$result = $this->Paginator->paginate('Customer');
我希望我可以帮助一些不想要 javascript hack 的人,但确实有可能在一侧有多个分页。
目前你只能使用'paramType' => 'named'
在我看来,PaginationComponent 和 PaginationHelper 中的原始代码很难阅读、扩展和适应。