John
John

Reputation: 513

Paginate With and Without Contain Key CakePHP 2

I am using CakePHP 2.x and would like to apply pagination in my controller but I would like it to be available in one case with a contain key and the other case without the contain key. As an example consider this example:

class BlogController extends AppController {
    public $name = "Blog";
    public $paginate = array(
    'Post'=>array(
            'limit'=>30, 
            'conditions'=>array('publish'=>1),
            ),
        );

    function just_posts() {
        // I want to paginate Post as it is
    }

    function posts_with_comments() {
        // I want paginate Post with 'contain'=>'comments'
    }
}

In my real life case the purpose in doing this is for performance, to reduce the query time. But I am at a loss how to implement this. The $this->paginate(...) will only accept an argument to filter records. Is there a way to make two paginators available for the same model in a controller?

Upvotes: 0

Views: 254

Answers (1)

drmonkeyninja
drmonkeyninja

Reputation: 8540

You should be able to modify your controller's paginate property within the relevant action to include the contain before doing the paginate:-

$this->paginate['Post']['contain'] = 'Comment';

This would extend the controller's defaults for the specific action.

Upvotes: 1

Related Questions