Browse Source

Correct use criteria with use count and join.

namespace
Alexander Demidov 11 years ago
parent
commit
d710b19fed
  1. 13
      model/SqlCriteria.php

13
model/SqlCriteria.php

@ -70,6 +70,7 @@ class SqlCriteria
$this->sql_expression = 'SELECT ' . $select . ' FROM :table'; $this->sql_expression = 'SELECT ' . $select . ' FROM :table';
} }
$this->sql_expression .= ' ' . implode(' ', $this->sql_join_expressions); $this->sql_expression .= ' ' . implode(' ', $this->sql_join_expressions);
$this->sql_join_expressions = null;
} }
} }
@ -80,13 +81,17 @@ class SqlCriteria
public function count() 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 { } else {
$this->sql_expression = 'SELECT COUNT(*) as count FROM :table'; $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) private function defineJoinTablePlaceholder($table_name)

Loading…
Cancel
Save