@vinhluu,
You could use something like :
$post = Gas::factory('post')->select("COUNT(*) AS stat")
->like('title', 'something')
->limit(1)
->all();
echo 'Total post(s) contain "something" :'.$post->stat;But frankly, the gas model and therefore, its API, was design to work with object and entity, so i think it will appropriate to have some method to handle those statistic task in corresponding model, rathen than having above cumbersome syntax everywhere in your script just to count the result. I will probably having something like this in ‘post’ model for example :
public function count_like($collumn, $value)
{
// In your gas model scope
// $this->db() was analog of $this->db (in native CI model)
return $this->db()->like($collumn, $value)
->from($this->table)
->count_all_results();
}
// Then just call it with :
// Gas::factory('post')->count_like('title', 'something');
It would be nice to have a finder function ‘count_where’ as a part of the core. This ‘count_where’ would mainly be used in obtaining pagination recordset.
Example:
$sqlCondition= " title like '%" . $this->input->get("keyword") . "%'";
// get match count
$resultCount = Gas::factory('post')->count_where($sqlCondition);
if($resultCount > 0)
{
// get paged result set
$currentPage = ($this->input->get('page') <> "" ? $this->input->get('page'): 0 );
$config['total_rows'] = $resultCount;
$this->pagination->initialize($config);
$paging = $this->pagination->create_links() ;
$limit = 20;
$offset = $currentPage * $limit;
$resultSet = Gas::factory('post')->find_where($sqlCondition, $limit, $offset);
}
