Where to add pagination page count in pagination?

I am trying to change the default value of 10 for the number of records to show on each page. I tried following different techniques but found none of them to work. It seems that it has changed during different versions of Yii.

I am using version 1.1.2.

I want to change my invoice view of ?r=invoice/index and ?r=invoice/admin to show 20 records instead of 10. It looks like the way I should achieve this is by changing actionIndex() to this:


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Invoice',

			array(

				'criteria'=>array(

				'pagination'=>array('pageSize'=>20,),

				'with'=>array('client'),

				'condition'=>'businessId='. Yii::app()->userInfo->business,

				),

			)

		);

		

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

			'dataProvider'=>$dataProvider,

		));

	}

However this doesn’t change anything. Is my approach correct for the version I’m using?

Pagination should not be in Criteria, but in the main array (is a property of CDataProvider, not of CDbCriteria):





$dataProvider=new CActiveDataProvider('Invoice',

                        array(

                               'criteria'=>array(

                                    'with'=>array('client'),

                                    'condition'=>'businessId='. Yii::app()->userInfo->business,

                                ),

                                'pagination'=>array('pageSize'=>20),

                        )

                );




Like that should work

I confirm that

Thanks for the responses guys.

You were right. However that only changes the count in invoice/index but not invoice/admin.

I was hoping that what is applied to CActiveDataProvider in method actionIndex() would be the default throughout the model.

Still looking.

I’m not sure if this is the proper way, but you can add the pagination to the search method of your model as well. Doing so will make the /admin table have your paginated data…

I guess you mean like this kcackler:


$criteria->limit = 1;


//I'm using 1 to make testing more obvious

I did some looking around and found that I need to set pagination to false for it to work but that doesn’t help either. I still want to keep pagination anyway.

See also my suggestion here, about setting default page size.

This may also be of interest (very useful post by user Mike).

/Tommy

Ummmm no :)

I meant, look at the search method in your model. You’ll see that it has the exact same CActiveDataProvider setup as your controller does in the index method. So you can set it up as


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

			'pagination'=>array('pageSize'=>20),

		));

And it will paginate the admin table as well.

To clarify even further, here is the entire search method from my PriceRange model




	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id,true);

		$criteria->compare('low',$this->low,true);

		$criteria->compare('high',$this->high,true);

		$criteria->compare('sort',$this->sort,true);

		$criteria->compare('active',$this->active);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

			'pagination'=>array('pageSize'=>20),

		));



Like I said - No idea if it is the "proper" way to do things, but it does do the job.

Thanks kcackler,

That did it. Adding the pagination code to model::search() changes the row count for invoice/admin and adding the code to controller::actionIndex() changes the row count for invoice/index.

Thanks also to tri for commenting on how to achieve the global settings. I will look into that also.