Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set conditions for pagination

I'm trying to do custom paginates on CakePHP, the thing is that I want to pass custom conditions, something like this:

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

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

That's basically all the code involving pagination, but it doesn't work, it throws that it doesn't find the column People...

I just want to do paginations with custom conditions, that is, I want to set the conditions beforehand, is there a way to do that?

like image 282
WhiteFloater Avatar asked Dec 31 '25 21:12

WhiteFloater


2 Answers

Did you take a look at the documentation?

According the documentation you should add conditions like this:

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

So guess this should work:

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'));
}
like image 125
Erik van de Ven Avatar answered Jan 03 '26 11:01

Erik van de Ven


here is how I am doing it in cake 2.x

first of all be sure to include paginator component and helper in your controller, like

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

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

then in your action

$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');
like image 45
dav Avatar answered Jan 03 '26 09:01

dav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!