Cakephp两个单独的分页相同的模型 [英] Cakephp two separate paginations of the same model

查看:107
本文介绍了Cakephp两个单独的分页相同的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何处理这两个单独的分页在同一个视图。感谢您的帮助。

I have no idea how to handle these two separate paginations in the same view. Thanks for helping.

控制器代码:

[...]
$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);

[...]


推荐答案

p>我最近不得不做这个事情,因为我有一个HTML页面上有标签,在每个选项卡是不同的分页表的同一模型,每个不同的条件。

I recently had to do this very thing because I had a single HTML page that had tabs on it and in each tab was a different paginated table of the same model with different conditions for each.

我解决这个问题的方法是创建从我想要多次分页的模型派生的虚拟模型。

The way I worked around the problem was to create dummy models that derived from the model I wanted to paginate multiple times. Then I simply referenced those dummy models for my pagination.

例如:

基本模型

class Post extends appmodel { };

Dummy Models - 重要的是它们使用与基本模型相同的表格

Dummy Models - it is important that they use the same table as the base model

class Posts1 extends Post { var $useTable = 'posts'; }
class Posts2 extends Post { var $useTable = 'posts'; }

在您的控制器中

    function multiview($id = null) {

    $this->paginate['Posts1'] = array(
        'conditions'=>array('Posts1.field'=>0),
        'limit'=>5
    );
    $this->set('posts1', $this->paginate('Posts1'));

    $this->paginate['Posts2'] = array(
        'conditions'=>array('Posts2.field'=>1),
        'limit'=>5
    );
    $this->set('posts2', $this->paginate('Posts2'));
}

然后在您的视图中

Display first paginated data
<?php foreach ($posts1 as $post):   ?>
Do Paginated row display here...
<?php endforeach; ?>

<!-- Shows the page numbers -->
<?php echo $this->Paginator->numbers(array('model'=>'Posts1')); ?>
<!-- Shows the next and previous links -->
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
<!-- prints X of Y, where X is current page and Y is number of pages -->
<?php echo $this->Paginator->counter(); ?>

Display second paginated data

<?php foreach ($posts2 as $post):   ?>
Do Paginated row display here...
<?php endforeach; ?>

<!-- Shows the page numbers -->
<?php echo $this->Paginator->numbers(array('model'=>'Posts2')); ?>
<!-- Shows the next and previous links -->
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
<!-- prints X of Y, where X is current page and Y is number of pages -->
<?php echo $this->Paginator->counter(); ?>

这篇关于Cakephp两个单独的分页相同的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆