I am trying to make Cakephp paginate use the SQL_CALC_FOUND_ROWS function in mysql to return the number of full rows when using LIMIT. Hope this can eliminate the double request of paginateCount (), then paginate ().
I put this in my app_model.php and it basically works, but it can be done better. Can someone help me figure out how to override paginate / paginateCount so that it only runs 1 SQL stmt?
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$options = array_merge(compact('conditions', 'recursive'), $extra);
$options['fields'] = "SQL_CALC_FOUND_ROWS `{$this->alias}`.*";
Q: how do you get the RIGHT Limit value used in paginate ()?
$options['limit'] = 1; // ideally, should return same rows as in paginate()
Q: can we somehow get the query results for direct printing without an additional query?
$cache_results_from_paginateCount = $this->find('all', $options);
$found_rows = $this->query("/* do not cache for {$this->alias} */ SELECT FOUND_ROWS();");
$count = array_shift($found_rows[0][0]);
return $count;
}
public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$options = array_merge(compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'), $extra);
Q: - $cache_results_for_paginate paginateCount()?
return $cache_results_from_paginateCount;
return $this->find('all', $options);
}