CPagination doesn't work on a Model that uses a DefaultScope

Hi.

I have defined a Model, which uses the following DefaultScope:




public function defaultScope() {

  return array('group' => 'Ort'); // all results should be grouped by the field "Ort"

}



this works fine so far, but when i use the default view for listing objects, using the default Object Controller,

the Pager (CPagination) is missing.

When i dismiss the defaultScope, everything works fine.

This is a Bug in yii, isn’t it? I run yii-1.1-alpha.

Can we have a look at the action’s implementation in question?

It’s the default as generated from the crud command:

My Object is called "Stadt":

StadtController.php:




	public function actionList()

	{

		$criteria=new CDbCriteria;


		$pages=new CPagination(Stadt::model()->count($criteria));

		$pages->pageSize=self::PAGE_SIZE;

		$pages->applyLimit($criteria);


		$models=Stadt::model()->findAll($criteria);


		$this->render('list',array(

			'models'=>$models,

			'pages'=>$pages,

		));

	}



a print_r($models) returns the found models correctly.

I have seen, that the Pager is missing in the views/admin.php, too.

Ah! I just have found a nice hint: Stadt::model()->count($criteria) returns 1 - that’s wrong since there are about 8000 found without the DefaultScope.

Now, is a bug in the count function! (?) - or does the count function just ignore the DefaultScope? Then it is a missing feature :)

Placing a $pages=new CPagination(8000); works fine!

oops, sorry for double-posting this one. Is it possible to delete his own postings anyhow ?

Have you tried running the generated SQL in a database management software? Due to lack of aggregation functions I think you used grouping instead of ordering.

No, i intended to use sql GROUP. "Stadt" contains every City in Germany including their zipcode, and because almost every district in Germany uses an own zipcode, there often are more than 10 zipcodes for one city. Ordering them by sql GROUP only lists every available city once with the first valid zipcode. This is intended and working very good so far.

I can understand that the count(*) function can not be possible on every thinkable scope that could be assigned to a model, but GROUP is a very commonly used function, so i think it should be implemented?

Using the Yii trace function i come to the following conclusion:




 $criteria=new CDbCriteria;


    $pages=new CPagination(Stadt::model()->count($criteria));



produces the following query:




SELECT COUNT(*) FROM `Stadt` GROUP BY Ort;



which returns "6358 rows in set (0,01 sec)" times a 1.

Is it the fault of SQL returning a awkward result or the fault of Yii generating a wrong query???

Both and none.

Yii doesn’t consider the fact that COUNT is an aggregate function, which will return false results when used together with grouping.

MySQL does not interpret the COUNT(*) correctly, returning the first group.

I faced this problem today, my workaround was DAO. In your case:




Yii::app()->db->createCommand('SELECT COUNT(DISTINCT Ort) FROM Stadt')->queryScalar();