Yii Framework Forum: Broken pagination in CGridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Broken pagination in CGridview when using "with" in search() Rate Topic: -----

#1 User is offline   isla 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 10-May 12

Posted 19 May 2012 - 08:53 AM

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?
0

#2 User is offline   isla 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 10-May 12

Posted 20 May 2012 - 11:02 AM

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,
        ));
	}


0

#3 User is offline   Narretz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 20-August 11

Posted 20 May 2012 - 11:58 AM

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.
0

#4 User is offline   isla 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 10-May 12

Posted 21 May 2012 - 10:50 AM

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.
0

#5 User is offline   JaCk0 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 04-September 11

Posted 25 July 2012 - 05:13 AM

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?
0

#6 User is offline   Adi Priyanto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 20-July 12

Posted 27 September 2012 - 08:09 AM

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); }',
		),
	),
)); ?>

0

#7 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 27 September 2012 - 08:34 AM

Dear Friends

Check this wiki .Written with lot of purpose.

CGridView, CListView and CActiveDataProvider
0

#8 User is offline   Juanma04 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 06-February 13

Posted 06 February 2013 - 10:50 PM

Hi guys,

I've the same issue, any solution?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users