Browse Source

SqlCriteria - count - use $select parameter for preventions error with duplicate fields

namespace
Alexander Demidov 10 years ago
parent
commit
383396b96d
  1. 7
      Model/SqlCriteria.php
  2. 4
      Model/SqlModel.php

7
Model/SqlCriteria.php

@ -79,14 +79,15 @@ class SqlCriteria
return $this->model->find('', '', $this->where, null, null, null, null, 'DELETE FROM :table', $this->sql_expression_params)->affectedRows();
}
public function count()
public function count($select = 'x')
{
$sql_expression_backup = $this->sql_expression;
$this->select($select);
$this->defineJoinExpressions();
$sql_expression_backup = $this->sql_expression;
if (!$this->sql_expression) {
$this->sql_expression = 'SELECT COUNT(*) as count FROM :table';
}
$count = $this->model->count(array(), '', $this->where, null, $this->group_by, $this->sql_expression, $this->sql_expression_params)->fetchField('count');
$count = $this->model->count($select, '', $this->where, null, $this->group_by, $this->sql_expression, $this->sql_expression_params)->fetchField('count');
$this->sql_expression = $sql_expression_backup;
return $count;
}

4
Model/SqlModel.php

@ -226,14 +226,14 @@ 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)
public function count($select = 'x', $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())))
(($sql_expression) ? $sql_expression : ('SELECT * FROM ' . $this->identify($this->table())))
. (($where) ? (' WHERE ' . $where) : '')
. (($group_by) ? (' GROUP BY ' . $group_by) : '') . ') AS x',
$sql_expression_params,

Loading…
Cancel
Save