Permalink
Browse files

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

  • Loading branch information...
1 parent e56620c commit d68f4a0f2d05b9ca5b1689c1626c04eb87afa72d @MonkeyMaster MonkeyMaster committed Aug 10, 2012
Showing with 10 additions and 5 deletions.
  1. +10 −5 framework/db/ar/CActiveFinder.php
@@ -146,7 +146,7 @@ public function findAllBySql($sql,$params=array())
public function count($criteria)
{
Yii::trace(get_class($this->_joinTree->model).'.count() eagerly','system.db.ar.CActiveRecord');
- $this->joinAll=$criteria->together!==true;
+ $this->joinAll=$criteria->together===true;
$alias=$criteria->alias===null ? 't' : $criteria->alias;
$this->_joinTree->tableAlias=$alias;
@@ -704,15 +704,20 @@ public function findWithBase($baseRecords)
public function count($criteria=null)
{
$query=new CJoinQuery($this,$criteria);
- // ensure only one big join statement is used
- $this->_finder->baseLimited=false;
- $this->_finder->joinAll=true;
+
+ $this->_finder->baseLimited=!$this->_finder->joinAll;
$this->buildQuery($query);
+ $this->_finder->baseLimited=false;
+
+ $distinct=false;
+ foreach($this->children as $child)
+ if($child->_joined && !($child->relation instanceof CHasOneRelation || $child->relation instanceof CBelongsToRelation))
+ $distinct=true;
$select=is_array($criteria->select) ? implode(',',$criteria->select) : $criteria->select;
if($select!=='*' && !strncasecmp($select,'count',5))
$query->selects=array($select);
- else if(is_string($this->_table->primaryKey))
+ else if($distinct && is_string($this->_table->primaryKey))
{
$prefix=$this->getColumnPrefix();
$schema=$this->_builder->getSchema();

0 comments on commit d68f4a0

Please sign in to comment.