CakePHP uses two methods for managing pagination requests, which are paginate and paginateCount, they are used to get page data and the total number of records, respectively. In order for pagination to be performed with your custom queries, we will need to implement both of the above functions in our model file, where you want to have pagination to work with custom queries.
public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) { $recursive = -1; // Mandatory to have $this->useTable = false; $sql = ''; $sql .= "Your custom query here just do not include limit portion"; // Adding LIMIT Clause $sql .= (($page - 1) * $limit) . ', ' . $limit; $results = $this->query($sql); return $results; }
....
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) { $sql = ''; $sql .= "Your custom query here just do not include limit portion"; $this->recursive = $recursive; $results = $this->query($sql); return count($results); }
Then, finally, in the action of the controller
// Do not forgot to set this, not sure why $this->Asin->recursive = 0; // Setting up paging parameters $this->paginate = array('Asin'=>array('limit'=>5)); // Getting paginated result based on page
source share