From aa9ac8dfe821663a530aa287772eca6d8b85451e Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Thu, 19 Mar 2015 17:04:46 +0300 Subject: [PATCH] =?UTF-8?q?SqlCriteria=20-=20=D0=B8=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=20=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BB-=D0=B2=D0=B0=20=D0=B7=D0=B0=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B5=D0=B9=20=D0=BF=D1=80=D0=B8=20=D0=B8=D1=81=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B8=20?= =?UTF-8?q?GROUP=20BY=20(In=20this=20thread:=20http://www.cyberforum.ru/my?= =?UTF-8?q?sql/thread681915.html=20I=20have=20solution=20for=20my=20proble?= =?UTF-8?q?m=20-=20USE=20COUNT(DISTINCT=20pid))?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model/SqlCriteria.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Model/SqlCriteria.php b/Model/SqlCriteria.php index 8c7a9ec..b97e2fa 100644 --- a/Model/SqlCriteria.php +++ b/Model/SqlCriteria.php @@ -84,15 +84,22 @@ class SqlCriteria { $this->defineJoinExpressions($select); $sql_expression_backup = $this->sql_expression; + $group_by_backup = $this->group_by; $select_parent_query = 'COUNT(*) as count'; + $default_select = '*'; + if ($this->group_by) { + $default_select = 'DISTINCT ' . $this->group_by[0]; + } + $this->group_by = []; if (!$this->sql_expression) { - $this->sql_expression = 'SELECT COUNT(' . ($select ? $select : '*') . ') as count FROM :table'; + $this->sql_expression = 'SELECT COUNT(' . ($select ? $select : $default_select) . ') as count FROM :table'; $select_parent_query = 'x.count'; } else { - $this->sql_expression = preg_replace('#SELECT.+FROM#i', 'SELECT COUNT(' . ($select ? $select : '*') . ') as count FROM', $this->sql_expression); + $this->sql_expression = preg_replace('#SELECT.+FROM#i', 'SELECT COUNT(' . ($select ? $select : $default_select) . ') as count FROM', $this->sql_expression); } $count = $this->model->count($select_parent_query, $this->where, null, $this->group_by, $this->sql_expression, $this->sql_expression_params)->fetchField('count'); $this->sql_expression = $sql_expression_backup; + $this->group_by = $group_by_backup; return $count; }