If you have just came across the paginate helper in the Cake Docs, it seems that you can only define a general var $paginate array at the top of your controller. Unless all your actions are going to paginate the same results the same way, defining a general var $paginate is not going to get you very far. Here’s how you can get more control of by defining how each of a controller’s action is going to paginate.
Let’s say you have a model called Review and you have a ReviewController with an action ReviewsForProduct() which lists all reviews for a specific product.
function ReviewsForProduct($productid) { $this->paginate['Review'] = array( 'limit' => 15, 'order' => 'Rating.rating desc, Rating.created desc', 'contain' => array( 'Product', 'Rating' => array('fields' => array('id', 'rating', 'created')), ), 'fields' => array('id', 'product_id', 'content'), 'conditions' => array('Product.category'=>'3', 'Product.id'=>$productid) ); } $reviews = $this->paginate('Review'); $this->set(compact('reviews')); ?>
And in the ReviewsForProduct.ctp view, use the $paginator to paginate your reviews results as you normally would (nothing fancy here)
<?php echo $paginator->prev();?> | <?php echo $paginator->numbers();?> <?php echo $paginator->next();?>
This way you can free up yourself from having the same pagination rules being applied to all of your controller’s action. Just remember that the trick is $this->paginate[‘ModelNameHere’], all other filters (or should I say find params) such as limit, order, fields, contain and conditions can go in under it as an array as if you were doing a find.
Leave a Reply