diff --git a/model/SqlCriteria.php b/model/SqlCriteria.php index 72c8b23..a0e645d 100644 --- a/model/SqlCriteria.php +++ b/model/SqlCriteria.php @@ -70,6 +70,7 @@ class SqlCriteria $this->sql_expression = 'SELECT ' . $select . ' FROM :table'; } $this->sql_expression .= ' ' . implode(' ', $this->sql_join_expressions); + $this->sql_join_expressions = null; } } @@ -80,13 +81,17 @@ class SqlCriteria public function count() { - if ($this->sql_expression && strpos($this->sql_expression, 'COUNT(*) as count')) { - ; + $this->defineJoinExpressions(); + $sql_expression_backup = $this->sql_expression; + if ($this->sql_expression) { + $select = $this->model->getDb()->selectExpr($this->select, $this->distinct); + $this->sql_expression = str_replace($select, 'COUNT(*) as count', $this->sql_expression); } else { $this->sql_expression = 'SELECT COUNT(*) as count FROM :table'; } - $this->defineJoinExpressions(); - return $this->model->find(array(), '', $this->where, null, null, null, null, $this->sql_expression, $this->sql_expression_params)->fetchField('count'); + $count = $this->model->find(array(), '', $this->where, null, null, null, null, $this->sql_expression, $this->sql_expression_params)->fetchField('count'); + $this->sql_expression = $sql_expression_backup; + return $count; } private function defineJoinTablePlaceholder($table_name)