You need to implement cakephp custom pagination.
CakePHP uses two method to manage pagination queries, that are paginate and paginateCount, they are used to get page data and total
record count respectively. To have pagination work with your custom
queries, we will need to implement both above functions in our model
file where you want to have pagination to work with custom queries.
You can implement in your behavior file as well. Let’s see how we can
have this implemented.
public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$recursive = -1;
$this->useTable = false;
$sql = '';
$sql .= "SELECT * FROM table_name limit ";
$sql .= (($page - 1) * $limit) . ', ' . $limit;
$results = $this->query($sql);
return $results;
}
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$sql = '';
$sql .= "SELECT * FROM table_name";
$this->recursive = $recursive;
$results = $this->query($sql);
return count($results);
}
Your controller file will look like below:
---'my_pages' controller-----
public function view(){
$this->MyPages->recursive = 0;
$this->paginate = array('MyPages'=>array('limit'=>5));
$this->set('resultArray', $this->paginate('MyPages'));
}
Note : MyPages should be MyPage, because Cakephp model name should be singular.
Your view file will look like below:
----- 'view.ctp' file ------
foreach ($resultArray as $result){
echo $result['subject'];
echo $result['body'];
}
Also you can read the Pagination with Custom Queries in CakePHP