EllisLab/CodeIgniter — pull request #1982 by gpeltink (2012-11-07 12:46:54+00:00)
Title: Patch to skip reset for count_all_results()

This patch will add an option to not reset the query for count_all_results().

If you are building a long query with active record, it would be a shame if you need to do it again after you queried the result count.

Example:

// Array with a lot of search options
$array = array();

$this->db->select('*');
$this->db->from('tableName');

if (!empty($array['min_price'])) $this->db->where('price >=', $array['min_price']);
if (!empty($array['max_price'])) $this->db->where('price <=', $array['max_price']);
//...
if (!empty($array['finish'])) $this->db->where('finish', $array['finish']);
if (!empty($array['material'])) $this->db->where('material', $array['material']);

if (!empty($array['keyword'])) {
$this->db->like('title', $array['keyword']);
$this->db->or_like('description', $array['keyword']);
}

// Get the result count without resetting the query
$total = $this->db->count_all_results('', false);

// Add limit after count_all_results().
// Can't do it before otherwise you will get the limited result.
$this->db->limit(3, 1);

// Run query
$query = $this->db->get();

Without this patch you need to rebuild the complete query again after calling count_all_results().