Yii Framework Forum: CActiveRecord->count don't works with group and with - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CActiveRecord->count don't works with group and with Rate Topic: -----

#1 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 18 May 2010 - 03:11 AM

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.
0

#2 User is offline   icevan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 18-June 09
  • Location:Russia

Posted 18 May 2010 - 10:43 PM

ImHO
This problem is only a consequence of this trouble FindByPk and alias
PHP Ice Monster
0

#3 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 19 May 2010 - 04:13 AM

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

#4 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 20 May 2010 - 01:32 AM

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?
0

#5 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 24 May 2010 - 08:41 AM

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...
0

#6 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 26 May 2010 - 07:44 AM

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().
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users