From b7ff8ab3fc4bc7f5fb0657bf4d805a3fa786a9b3 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 26 Sep 2014 18:44:35 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=B2=20SqlCriteria.count()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model/SqlCriteria.php | 7 ++----- Model/SqlModel.php | 5 ++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Model/SqlCriteria.php b/Model/SqlCriteria.php index a0496ce..b81adde 100644 --- a/Model/SqlCriteria.php +++ b/Model/SqlCriteria.php @@ -79,17 +79,14 @@ class SqlCriteria return $this->model->find('', '', $this->where, null, null, null, null, 'DELETE FROM :table', $this->sql_expression_params)->affectedRows(); } - public function count($select = 'x') + public function count($select = null) { $this->defineJoinExpressions($select); $sql_expression_backup = $this->sql_expression; - if ($select) { - $this->select( $select ); - } if (!$this->sql_expression) { $this->sql_expression = 'SELECT COUNT(' . ($select ? $select : '*') . ') as count FROM :table'; } - $count = $this->model->count($select, '', $this->where, null, $this->group_by, $this->sql_expression, $this->sql_expression_params)->fetchField('count'); + $count = $this->model->count($this->where, null, $this->group_by, $this->sql_expression, $this->sql_expression_params)->fetchField('count'); $this->sql_expression = $sql_expression_backup; return $count; } diff --git a/Model/SqlModel.php b/Model/SqlModel.php index 95f7cbb..fe09f69 100644 --- a/Model/SqlModel.php +++ b/Model/SqlModel.php @@ -226,13 +226,12 @@ abstract class SqlModel extends Model return new SqlResultProvider($result); } - public function count($select = 'x', $distinct, $where,$heaving = null, $group_by = null, $sql_expression = null, $sql_expression_params = array(), $cache_key = null) + public function count($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 (' . + 'SELECT x.count FROM (' . (($sql_expression) ? $sql_expression : ('SELECT * FROM ' . $this->identify($this->table()))) . (($where) ? (' WHERE ' . $where) : '') . (($group_by) ? (' GROUP BY ' . $group_by) : '') . ') AS x',