设置分页条件

Set conditions for pagination

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

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

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

这基本上是所有涉及分页的代码,但它不起作用,它抛出找不到列 People...

我只想做自定义条件的分页,也就是想提前设置好条件,请问有什么办法吗?

你看过文档了吗?

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

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'));
}

好的,我解决了,我是这样做的: 首先我声明分页,如果你想它可以是空的:

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

然后,每当我想改变什么的时候,我就这样做:

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

这是我做蛋糕的方法2.x

首先确保在你的控制器中包含分页器组件和助手,比如

$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');