I found a bug in CACtiveRecord->count().
It return the wrong result if invoked with the having condition.
There is an issue opened for this bug (1244), in wich there is a small test.
I resolved by changing the code of CDbCommandBuilder at line 91:
/** * Creates a COUNT(*) command for a single table. * @param mixed the table schema ({@link CDbTableSchema}) or the table name (string). * @param CDbCriteria the query criteria * @param string the alias name of the primary table. Defaults to 't'. * @return CDbCommand query command. */ public function createCountCommand($table,$criteria,$alias='t') { $this->ensureTable($table); $select=is_array($criteria->select) ? implode(', ',$criteria->select) : $criteria->select; if($criteria->alias!='') $alias=$criteria->alias; $alias=$this->_schema->quoteTableName($alias); $sql=($criteria->distinct ? 'SELECT DISTINCT':'SELECT')." {$select} FROM {$table->rawName} $alias"; $sql=$this->applyJoin($sql,$criteria->join); $sql=$this->applyCondition($sql,$criteria->condition); $sql=$this->applyGroup($sql,$criteria->group); $sql=$this->applyHaving($sql,$criteria->having); $sql="SELECT COUNT(*) FROM ($sql) sq"; $command=$this->_connection->createCommand($sql); $this->bindValues($command,$criteria->params); return $command; }
This code works fine in any situation.
Can you tell me if is correct and if you will patch this on the framework?