Broken pagination in CGridview

Hi, I have a CGridview filled with data.

Everything works great except the pagination, when I click the next page only the count and pagenumber changes, not the actual values in the columns(they stay the same).

In my model, I tried to remove this code in the search() method:




$criteria->with = array('relation');



and then the pagination works.

I tried moving the piece of code around but it doesn’t work.

I need this code in the model for a specific search so I can’t remove it.

Any idea what’s causing this?

Noticed that when using $criteria->order the same problem appears.

Also,

when I use $criteria->condition = ‘foo = 0’; in the search method, the pagination throws an error when I click on it.

Am I using "criteria" wrong?

here is an example of my model:




public function search()

	{

		$criteria=new CDbCriteria;

		

		$criteria->condition = 'foo = 0';

		$criteria->with = array('foobar');

		$criteria->order = 'bar ASC';


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

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


		if ($this->search == self::CHOICE_1)

        {

            $criteria->addInCondition('foobar.name', array('1'));

        }


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

                'criteria' => $criteria,

        ));

	}




Not really sure. Please provide the relevant error.

When you use compare and condition together, it can happen that the criteria overwrite each other. Have you tried $criteria->compare(‘foo’, 0)?

And what is this:




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

                'criteria' => $criteria,

        ));

Why get_class($this)? Default implementation is just $this.

I noticed that when I removed $criteria->with the order is now working. So the problem lies within ‘with’ I guess.

This is a part of the error that I get when I click on the next page in the pagination (when I include ‘with’ in the search() method) :




Error 500: CDbException

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 207 General SQL Server error: Check messages from the SQL Server [207] (severity 16) [(null)]. The SQL statement executed was: SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 20 [t].[bar_id] AS [t0_c0], [t].[foo_id] AS [t0_c1], [t].[name] AS [t0_c2],



I also changed get_class($this) to $this.

I have problems with pagination when I use with in CDbCriteria object in search method. My search method code:


public function search()

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;

		$criteria->with = array('source');

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

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

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

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

		$criteria->compare('source.message',$this->mensaje,true);




		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'sort'=>array(

				'attributes'=>array(

					 'mensaje'=>array(

						'asc'=>'source.message',

						'desc'=>'source.message DESC',

					 ),

					'*'

				 )

			)

		));

	}

Removing $criteria->with = array(‘source’); fix pagination but search relation not work. Any idea what’s causing this behavior?

I have the same problem with you.

But i found something. If I sort one of the columns and the I click the page links, the pagination works fine.

Here is my code, if you’re interested.




IN MODELS:

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'fparent_id0' => array(self::BELONGS_TO, 'Menu', 'fparent_id'),

			'fstatus0' => array(self::BELONGS_TO, 'Status', 'fstatus'),

		);

	}

	




	public function search()

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;

		

		$criteria->with = array('fparent_id0','fstatus0');

		$criteria->addCondition('t.fparent_id <> 0');

		$criteria->compare('t.fid',$this->fid);

		$criteria->compare('fparent_id0.fname',$this->fparent_id,true);

		$criteria->compare('t.fname',$this->fname,true);

		$criteria->compare('t.flink',$this->flink,true);

		$criteria->compare('fstatus0.fname',$this->fstatus);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'sort'=>array(

				'attributes'=>array(

					'fparent_id'=>array(

						'asc'=>'fparent_id0.fname',

						'desc'=>'fparent_id0.fname DESC',

					),

					'fstatus'=>array(

						'asc'=>'fstatus0.fname',

						'desc'=>'fstatus0.fname DESC',

					),

					'*'

				),

			),

		));

	}


IN CONTROLLER:

	public function actionAdmin()

	{

		$model=new Submenu('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Submenu']))

			$model->attributes=$_GET['Submenu'];


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

			'model'=>$model,

		));

	}


IN VIEW:

<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'submenu-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'fid',

		array( 'name'=>'fparent_id', 'value'=>'$data->fparent_id0->fname'),

		'fname',

		'flink',

		array( 'name'=>'fstatus', 'value'=>'$data->fstatus0->fname'),

		array(

			'class'=>'CButtonColumn',

			'afterDelete'=>'function(link,success,data){ if(success) $("#statusMsg").html(data); }',

		),

	),

)); ?>



Dear Friends

Check this wiki .Written with lot of purpose.

CGridView, CListView and CActiveDataProvider

Hi guys,

I’ve the same issue, any solution?

I’ve encountered the same problem. Hasn’t anybody figured it out by now?