Correct detect count records in SQlCriteria with use group by

This commit is contained in:
2014-07-28 11:50:27 +04:00
parent 15ea37109d
commit 2a98f9b0d7
2 changed files with 17 additions and 8 deletions

View File

@ -226,6 +226,22 @@ abstract class SqlModel extends Model
return new SqlResultProvider($result);
}
public function count($select, $distinct, $where,$heaving = null, $group_by = null, $sql_expression = null, $sql_expression_params = array(), $cache_key = null)
{
$select = $this->db->selectExpr($select, $distinct);
$where = $this->db->whereExpr($where);
$group_by = $this->db->groupByExpr($group_by);
$result = $this->query(
'SELECT COUNT(*) as count FROM (' .
(($sql_expression) ? $sql_expression : ('SELECT ' . $select . ' FROM ' . $this->identify($this->table())))
. (($where) ? (' WHERE ' . $where) : '')
. (($group_by) ? (' GROUP BY ' . $group_by) : '') . ') AS x',
$sql_expression_params,
$cache_key
);
return new SqlResultProvider($result);
}
/**
* @param $sql_expression null
* @param $sql_expression_params array