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?
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…
$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.
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),
));
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.
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.