Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote-tracking branch 'refs/remotes/origin/master'

  • Loading branch information...
commit cbd4d03f0563b57c0744710b0d1ebe20cb7b3dc5 2 parents 0eec059 + 1279019
@samdark samdark authored
View
1  CHANGELOG
@@ -3,6 +3,7 @@
Version 1.1.14 work in progress
-------------------------------
+- Bug #135: Fixed wrong CActiveRecord rows count with having (klimov-paul)
- Bug #150: Fixed CWidget was not switching between view paths when using themes (antoncpu)
- Bug #1464: Fixed transparent background for ImageMagick in CCaptchaAction (manuel-84, cebe)
- Bug #1693: Fixed log file will rotate twice when high performance (monque)
View
39 framework/db/ar/CActiveFinder.php
@@ -699,23 +699,36 @@ public function count($criteria=null)
$this->_finder->joinAll=true;
$this->buildQuery($query);
- $select=is_array($criteria->select) ? implode(',',$criteria->select) : $criteria->select;
- if($select!=='*' && !strncasecmp($select,'count',5))
- $query->selects=array($select);
- elseif(is_string($this->_table->primaryKey))
+ $query->limit=$query->offset=-1;
+
+ if(!empty($criteria->group) || !empty($criteria->having))
{
- $prefix=$this->getColumnPrefix();
- $schema=$this->_builder->getSchema();
- $column=$prefix.$schema->quoteColumnName($this->_table->primaryKey);
- $query->selects=array("COUNT(DISTINCT $column)");
+ $query->orders = array();
+ $command=$query->createCommand($this->_builder);
+ $sql=$command->getText();
+ $sql="SELECT COUNT(*) FROM ({$sql}) sq";
+ $command->setText($sql);
+ return $command->queryScalar();
}
else
- $query->selects=array("COUNT(*)");
+ {
+ $select=is_array($criteria->select) ? implode(',',$criteria->select) : $criteria->select;
+ if($select!=='*' && !strncasecmp($select,'count',5))
+ $query->selects=array($select);
+ elseif(is_string($this->_table->primaryKey))
+ {
+ $prefix=$this->getColumnPrefix();
+ $schema=$this->_builder->getSchema();
+ $column=$prefix.$schema->quoteColumnName($this->_table->primaryKey);
+ $query->selects=array("COUNT(DISTINCT $column)");
+ }
+ else
+ $query->selects=array("COUNT(*)");
- $query->orders=$query->groups=$query->havings=array();
- $query->limit=$query->offset=-1;
- $command=$query->createCommand($this->_builder);
- return $command->queryScalar();
+ $query->orders=$query->groups=$query->havings=array();
+ $command=$query->createCommand($this->_builder);
+ return $command->queryScalar();
+ }
}
/**
View
28 tests/framework/db/ar/CActiveRecordTest.php
@@ -1401,4 +1401,32 @@ public function testIssue507()
}
+ /**
+ * @see https://github.com/yiisoft/yii/issues/135
+ */
+ public function testCountWithHaving()
+ {
+ $criteriaWithHaving = new CDbCriteria();
+ $criteriaWithHaving->group = 'id';
+ $criteriaWithHaving->having = 'id = 1';
+ $count = Post::model()->count($criteriaWithHaving);
+
+ $this->assertEquals(1, $count, 'Having condition has not been applied on count!');
+ }
+
+ /**
+ * @see https://github.com/yiisoft/yii/issues/135
+ */
+ public function testCountWithHavingRelational()
+ {
+ $criteriaWithHaving = new CDbCriteria();
+ $criteriaWithHaving->select = 't.id AS test_field';
+ $criteriaWithHaving->with = array('author');
+ $criteriaWithHaving->group = 't.id';
+ $criteriaWithHaving->having = 'test_field = 1';
+ $count = Post::model()->count($criteriaWithHaving);
+
+ $this->assertEquals(1, $count, 'Having condition has not been applied on count with relation!');
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.