One way to keep your search logic in the model, and still paginate in the controller is to do this:
Explanation:
Instead of returning the actual results from the model, just return any/all find options, then do the pagination like you normally would. For some examples, like this simple one below, it might seem overkill, but it leaves room for adding a lot more options to your find()
like contain
, order
, group
, joins
, conditions
... etc etc. And stays more in-line with the "Fat Models, Skinny Controllers" mantra.
It's also nice to set up your find()
s with options like this so it's easily re-usable throughout your site - just pass it different options and you're good to go.
Code:
/* CONTROLLER
*/
$opts = array('paginate' => true, 'limit'=>20);
$paginateOptions = $this->Event->getEvents($opts);
$this->paginate = $paginateOptions;
$data = $this->paginate('Event');
/* MODEL
*/
public function getProducts($opts = null) {
$params = array();
//limit
$params['limit'] = 50; //default
if(!empty($opts['limit'])) $params['limit'] = $opts['limit'];
//paginate option
$paginate = false;
if(isset($opts['paginate'])) {
if($opts['paginate']) $paginate = true;
}
//either return the options just created (paginate)
if($paginate) {
return $qOpts;
//or return the events data
} else {
$data = $this->find('all', $qOpts);
return $data;
}
}
There are ways to write this a bit slimmer / less lines of code - but I like writing it like this so it's quickly understandable.
(There doesn't appear to be anything wrong with your overall structure.)