Cakephp how to set limit to assocation model?

188 views Asked by At

How can I set limit to association model ("Comment" model in below code) when using paginator component. I am using below code but not working :

$this->Paginator->settings = array(
            'Post' => array(
                    'recursive' => 1,
                    'conditions' => $conditions,
                    'limit' => 10,                    
            ),
            'Comment' => array(
                    'limit' => 1
            )
    );
2

There are 2 answers

0
kicaj On

You have Post hasMany Comment?
If yes, You can set limit by variable $hasMany in Post model:

class Post extends AppModel {
    public $hasMany = array(
        'Comment' => array(
            'className' => 'Comment',
            //...
            'limit' => '1'));
}
0
Mindaugas Norvilas On

You should use Containable behavior (in Post model):

public $actsAs = array('Containable');

Then, your Paginator settings should look like:

$this->Paginator->settings = array(
        'contain' => array(
            'Comment' => array(
                'limit' => 1
            )
        ),
        'conditions' => $conditions,
        'limit' => 10,
    );