Skip to content
This repository
Browse code

Fixes #108: CActiveRecord::count() ignore 'together' relation option

  • Loading branch information...
commit d68f4a0f2d05b9ca5b1689c1626c04eb87afa72d 1 parent e56620c
MonkeyMaster authored August 10, 2012

Showing 1 changed file with 10 additions and 5 deletions. Show diff stats Hide diff stats

  1. 15  framework/db/ar/CActiveFinder.php
15  framework/db/ar/CActiveFinder.php
@@ -146,7 +146,7 @@ public function findAllBySql($sql,$params=array())
146 146
 	public function count($criteria)
147 147
 	{
148 148
 		Yii::trace(get_class($this->_joinTree->model).'.count() eagerly','system.db.ar.CActiveRecord');
149  
-		$this->joinAll=$criteria->together!==true;
  149
+		$this->joinAll=$criteria->together===true;
150 150
 
151 151
 		$alias=$criteria->alias===null ? 't' : $criteria->alias;
152 152
 		$this->_joinTree->tableAlias=$alias;
@@ -704,15 +704,20 @@ public function findWithBase($baseRecords)
704 704
 	public function count($criteria=null)
705 705
 	{
706 706
 		$query=new CJoinQuery($this,$criteria);
707  
-		// ensure only one big join statement is used
708  
-		$this->_finder->baseLimited=false;
709  
-		$this->_finder->joinAll=true;
  707
+
  708
+		$this->_finder->baseLimited=!$this->_finder->joinAll;
710 709
 		$this->buildQuery($query);
  710
+		$this->_finder->baseLimited=false;
  711
+
  712
+		$distinct=false;
  713
+		foreach($this->children as $child)
  714
+			if($child->_joined && !($child->relation instanceof CHasOneRelation || $child->relation instanceof CBelongsToRelation))
  715
+				$distinct=true;
711 716
 
712 717
 		$select=is_array($criteria->select) ? implode(',',$criteria->select) : $criteria->select;
713 718
 		if($select!=='*' && !strncasecmp($select,'count',5))
714 719
 			$query->selects=array($select);
715  
-		else if(is_string($this->_table->primaryKey))
  720
+		else if($distinct && is_string($this->_table->primaryKey))
716 721
 		{
717 722
 			$prefix=$this->getColumnPrefix();
718 723
 			$schema=$this->_builder->getSchema();

0 notes on commit d68f4a0

Please sign in to comment.
Something went wrong with that request. Please try again.