设置分页条件

时间:2015-01-14 14:30:08

标签: cakephp pagination paginate

我试图在CakePHP上做自定义分页,问题是我想传递自定义条件,如下所示:

$conditions['People'] = $this->People->find('all',
    array(
        'order' => array('People.id DESC'),
        'limit' => 16, 
        'order' => 'People.id DESC'
    )
);

$people = $this->paginate('People',$conditions);

基本上所有代码都涉及分页,但它不起作用,它会导致它找不到列人......

我只是想用自定义条件进行分页,也就是说,我想事先设置条件,有没有办法做到这一点?

3 个答案:

答案 0 :(得分:4)

您是否看过文档?

根据文档,您应该添加以下条件:

public function list_recipes() {
    $this->Paginator->settings = array(
        'conditions' => array('Recipe.title LIKE' => 'a%'),
        'limit' => 10
    );
    $data = $this->Paginator->paginate('Recipe');
    $this->set(compact('data'));
}

所以猜这应该有效:

public function list_people() {
    $this->Paginator->settings = array('order' => array('People.id DESC'),'limit' => 16,'order' => 'People.id DESC');
    $data = $this->Paginator->paginate('People');
    $this->set(compact('data'));
}

答案 1 :(得分:4)

这是我在蛋糕2.x

中的表现

首先要确保在控制器中包含paginator组件和帮助器,例如

$public $components = array(
  'Paginator'
);

$public $helpers = array(
  'Paginator'
);

然后在你的行动中

$options = array(
    'conditions' => array(
        'Post.status' => 1
    ),
    'fields' => array(
        'Post.id',
        'Post.title',
        'Post.created'
    ),
    'order' => array(
        'Post.created' => 'DESC'
    ),
    'limit' => 10
);

$this->Paginator->settings = $options;
$posts = $this->Paginator->paginate('Post');

答案 2 :(得分:3)

好的,我解决了,这就是我做的: 首先我声明了paginate,如果你想要它可以为空:

public $paginate = array('People'=>array(
        'limit' => 6,
        'order' => 'People.id DESC'
    )); 

然后,每当我想要改变某些东西时,我就会这样做:

$this->paginate['People']['limit'] = 12;