gpt4 book ai didi

Cakephp 同型号的两个独立分页

转载 作者:行者123 更新时间:2023-12-01 01:35:38 25 4
gpt4 key购买 nike

我不知道如何在同一个 View 中处理这两个单独的分页。谢谢你的帮助。

Controller 代码:

[...]
$this->Post->bindModel(array(
'hasAndBelongsToMany'=>array('Tag'),
'belongsTo'=>array('User')),false);
if($this->Session->check('Auth.User.id'))
$this->Post->bindModel(array(
'hasMany'=>array(
'UserVote'=>array('conditions'=>array('UserVote.user_id'=>$this->Session->read('Auth.User.id') )),
'Favorite'=>array('conditions'=>array('Favorite.user_id'=>$this->Session->read('Auth.User.id') ))
)), false);


$posts = $this->paginate($this->Post,array('Post.public'=>1,'Post.user_id'=>$uid));

$posts2 = $this->paginate($this->Post,array('Post.public'=>0,'Post.user_id'=>$uid));


$this->set('posts',$posts);
$this->set('posts2',$posts2);

[...]

最佳答案

尽管在大多数情况下,正确的解决方案是重新构建您的 UI 以消除对双分页的需要,但以下是一个可行的解决方案:

首先,在您的 Controller 中覆盖 Cake 的 paginate() 函数以查找分页键:

    /**
* Handles automatic pagination of model records.
*
* @param mixed $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
* @param mixed $scope Conditions to use while paginating
* @param array $whitelist List of allowed options for paging
* @return array Model query results
* @access public
* @link http://book.cakephp.org/view/165/Controller-Setup
*/
function paginate($object = null, $scope = array(), $whitelist = array(), $key = null) {
$results = parent::paginate($object, $scope, $whitelist);
if ($key) {
$this->params['paging'][$key] = $this->params['paging'][$object];
unset($this->params['paging'][$object]);
}

return $results;
}

然后
   /**
* undocumented function
*
* @param string $key
* @return void
* @access public
*/
function _pageForPagination($by) {
$page = 1;
$samekey = isset($this->params['named']['by']) && $this->params['named']['by'] == $by;
$pageInUrl = isset($this->params['named']['page']);
if ($samekey && $pageInUrl) {
$page = $this->params['named']['page'];
}

$this->passedArgs['page'] = $page;
return $page;
}

/**
* FIXME: Wrapper for Cake's pagination
* Change pagination criteria on the fly (conditions, grouping, order, limit)
*
* @param string $model
* @param string $criteria
* @return void
* @author Andrew
*/
function _paginateBy($key) {
$this->User->unbindModel(array('hasMany' => array('UserImage')), false);
$this->paginate['User'] = am($this->User->getCriteria($key), array('page' => $this->_pageForPagination($key)));
return $this->paginate('User', array(), array(), $key);
}

然后在 Controller 中像这样使用它:
$this->set('byJoinDate', $this->_paginateBy('random'));

在模型中:
echo $paginator->prev('prev', array('model' => $by, 'class' => 'back'), null, array('model' => $by, 'class' => 'disabled背部'));

关于Cakephp 同型号的两个独立分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2305839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com