CActiveRecord->count don't works with group and with

I found a bug in CActiveRecord.

I have 2 tables in foreign key, log table BelongsTo cookie on cookie ID.

When I do like that:




		$dataProvider=$model->search();

		$dataProvider->criteria->select='cookie_id, max(t.date) as date, count(*) as page';


		$dataProvider->criteria->group= 'cookie_id';




The dataProvder provide the corrected data, grouped by cookie ID, but the pagination shows the number of item without groupBy.

By commenting the line


$dataProvider->criteria->with= array('cookie');

the result provided is correct.

I give a look to CActive record, and I noticed that at the line 1363




		if(empty($criteria->with))

			return $builder->createCountCommand($this->getTableSchema(),$criteria)->queryScalar();

		else

			return $this->with($criteria->with)->count($criteria);



If I change in




			return $builder->createCountCommand($this->getTableSchema(),$criteria)->queryScalar();



the result is once again correct. Of corse that cannot be a solution, but maybe can be a tip.

ImHO

This problem is only a consequence of this trouble FindByPk and alias

This bug don’t rise an exception, simply return the wrong result

One more bug in CActiveRecord->count();

The having condition is not taken in account while doing count().

That cannot be an expected behaviour, because




CActiveRecord->count($criteria);

sizeOf(CActiveRecord->findAll($criteria));



are expected to return the same result (of corse $criteria are intended without LIMIT).

I noticed that the CActiveRecord->count() create a sql query like "select count (DISTINCT tableId)", why is like that?

I guess that can be much more simple and effective to use a subquery, like




SELECT count(*)

FROM ($sqlStatementWithoutLimit) as foo



That is more simple and ensure that the result of count and sizeof() will be the same.

What do you think about it?

Hi guys!!!

I opened an issue for this bug, but still no answer…

I have a project completed with some mistakes because of this bug, I’d like to know at least if you will fix in the next time or if I should bypass hardcoding queries…

Hallo all.

I bypassed this problem in my application by doing the query using CDbCriteria->join instead of CDbCriteria->with.

That not resolve the bug, but it become an "anyone problem" for me.

I opened a new topic and an issue about another problem of CActiveRecord->count().