Browse Source

Исправление ошибки в SqlCriteria.count() для запросов в которых есть свой индивидуальный select и groupby

namespace
Alexander Demidov 10 years ago
parent
commit
67f47b882b
  1. 2
      Model/SqlCriteria.php
  2. 4
      Model/SqlModel.php

2
Model/SqlCriteria.php

@ -83,8 +83,10 @@ class SqlCriteria
{ {
$this->defineJoinExpressions($select); $this->defineJoinExpressions($select);
$sql_expression_backup = $this->sql_expression; $sql_expression_backup = $this->sql_expression;
$select = 'COUNT(*) as count';
if (!$this->sql_expression) { if (!$this->sql_expression) {
$this->sql_expression = 'SELECT COUNT(' . ($select ? $select : '*') . ') as count FROM :table'; $this->sql_expression = 'SELECT COUNT(' . ($select ? $select : '*') . ') as count FROM :table';
$select = 'x.count';
} }
$count = $this->model->count($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; $this->sql_expression = $sql_expression_backup;

4
Model/SqlModel.php

@ -226,12 +226,12 @@ abstract class SqlModel extends Model
return new SqlResultProvider($result); return new SqlResultProvider($result);
} }
public function count($where, $heaving = null, $group_by = null, $sql_expression = null, $sql_expression_params = array(), $cache_key = null)
public function count($select = 'x.count', $where, $heaving = null, $group_by = null, $sql_expression = null, $sql_expression_params = array(), $cache_key = null)
{ {
$where = $this->db->whereExpr($where); $where = $this->db->whereExpr($where);
$group_by = $this->db->groupByExpr($group_by); $group_by = $this->db->groupByExpr($group_by);
$result = $this->query( $result = $this->query(
'SELECT x.count FROM (' .
'SELECT ' . $select . ' FROM (' .
(($sql_expression) ? $sql_expression : ('SELECT * FROM ' . $this->identify($this->table()))) (($sql_expression) ? $sql_expression : ('SELECT * FROM ' . $this->identify($this->table())))
. (($where) ? (' WHERE ' . $where) : '') . (($where) ? (' WHERE ' . $where) : '')
. (($group_by) ? (' GROUP BY ' . $group_by) : '') . ') AS x', . (($group_by) ? (' GROUP BY ' . $group_by) : '') . ') AS x',

Loading…
Cancel
Save