I haven’t come across this before but there seems to be an issue with CActiveDataProvider and the totalItemCount variable when using group by clause in criteria. I have a simple table set up like this…
CREATE TABLE `IndexTable` (
`index_sid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`keyword` varchar(120) DEFAULT NULL,
`position` int(10) unsigned NOT NULL,
`object_id` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`index_sid`)
) ENGINE=InnoDB CHARSET=utf8;
In it there are a total of 9 records and two distinct object_id values (1 and 2).
The item counts are wrong which in turn messes up the pagination in CGridView.
var_dump($dp->totalItemCount) // returns 9 (the total row count of the table
var_dump($dp->itemCount) // return 2 - the correct amount
When used in CGridView, the text at the top displays "Displaying 1-1 of 9 result(s)." and the pagination items go from 1-9 (as expected with the totalItemCount being 9) but it should be pageination of 1-2 and display text being "Displaying 1-1 of 2 result(s)."
Thanks for the reply. Tried the solution on the link but still not resolved.
I’ve even tried to simplify it further…
$criteria = new CDbCriteria();
$criteria->group = 'object_id';
echo count(IndexTable::model()->findAll($criteria)); // return 2 - CORRECT
$dp = new CActiveDataProvider('IndexTable',array('criteria'=>$criteria));
echo $dp->totalItemCount; // return 9 - INCORRECT (that is the total row count of the table
Either the fix was lost again at some point or not fully implemented. The following code produces the wrong total record count and hence the wrong pagination in CGridView …
$criteria=new CDbCriteria;
$criteria->select='ownerName1, SUM(amount) as taxBalance';
$criteria->group='ownerName1';
$criteria->compare('ownerName1',$this->ownerName1,true);
$criteria->compare('taxBalance',$this->taxBalance,true);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'sort'=>array('defaultOrder'=>'ownerName1 ASC'),
));
Also the compare criteria on the SUM(amount) does not work, so CGridView cannot search on taxBalance.
Am I doing something wrong or should I bug report this ?
I have a $criteria->group clause in the criteria passed to the CActiveDataProvider. I see the GROUP BY in the data SQL query, but not in the count SQL query.
As a result the total count is wrong, and pagination is wrong too.
I saw a message saying that there’s a fix in SVN - can anyone instruct me how to get this fix?
At this moment I am having the same issue! The totalItemCount does not use the GROUP BY in the SQL statetement so the total amount is not correct. Anyone knows a fix?