Related model in CGridView Issue

Hi everyone I recently found the wiki Searching and sorting by related model in CGridView in the following link http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview … now i got the problem that when I change the page of the CGridview doesnt change it always shows the same content I’ve tried with two solutions but the result is the same and also with different Databases one in MySQL and one in MSSQL well I hope you could help me i will appreciate a lot, thanks in advice

Regards

JD

Please post your codes. I believe nobody can help you with only the description. Also check with Firebug if there is any javascript error.




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

	'id'=>'productos-cliente-grid',

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

	'filter'=>$model,

	'columns'=>array(

		array(

            'name'=>'idProducto',

            'value'=>'$data->producto->nombre',

            'filter'=>Productos::model()->options,

        ),

		'idProducto',

		'codigoCliente',

		array(

			'class'=>'CButtonColumn',

		),

	),

));







 public function search()

    {

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

        // should not be searched.


        $criteria=new CDbCriteria;

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

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

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

 

		$criteria->compare('producto.nombre',$this->producto_search,true);

		

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

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

            'criteria'=>$criteria,

        ));

    }




public function rules()

    {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

           .......

            // Please remove those attributes that should not be searched.

            array('idProdClient, idProducto, codigoCliente, activo, idConfigProd, producto_search', 'safe', 'on'=>'search'),

			

        );

    }




At first look, I don’t see what is wrong. Please perform these checks:

[list=1]

[*]Can you post your controller action?

[*]Does the total number displayed in the summary (above the grid) reflect your database results?

[*]Can you check the jQuery code associated with the gridview in the output HTML?

[/list]

PS you can rewrite two lines as


$criteria->with=array('producto', 'cliente');

Thanks for helping me out

My controller action looks like this


public function actionAdmin()

	{

		$model=new ProductosCliente('search');

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

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

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


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

		//	'model'=>$model,

		//));

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

			'model'=>$model,

		),false,true);

	}



  1. Does the total number displayed in the summary (above the grid) reflect your database results?

Yes, the number of displayed results is the same in the database, but the content doesnt change when i change page.

  1. Can you check the jQuery code associated with the gridview in the output HTML?

jQuery('#productos-cliente-grid').yiiGridView({'ajaxUpdate':['productos-cliente-grid'],'ajaxVar':'ajax','pagerClass':'pager','loadingClass':'grid-view-loading','filterClass':'filters','tableClass':'items','selectableRows':1,'pageVar':'ProductosCliente_page'});



Can you please re-test with simpler code to see if it works:


/* Controller */

public function actionAdmin()

{

    $model=new ProductosCliente('search');

    $model->unsetAttributes();

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

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

    // using render instead of renderPartial

    $this->render('admin', array('model'=>$model));

}


/* Model */

public function search()

{

    $criteria=new CDbCriteria;

    // using $this instead of get_class($this)

    return new CActiveDataProvider($this, array('criteria'=>$criteria));

}


/* View */

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

    'id'=>'productos-cliente-grid',

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

    'filter'=>$model,

    'columns'=>array(

        'idProducto',

        'codigoCliente',

        array(

            'class'=>'CButtonColumn',

        ),

    ),

));



I still got the same issue the content doesnt change at all :( any other ideas?

I’m baffled… Here are further checks:

  1. Has any CGridView worked for you before?

  2. Can you debug the js code?

  3. Can you put your page online?

  4. Can you post a screenshot?

As i mentioned before I’ve tried with different databases and different projects I dont know if you could send me a full example of how you do it? thanks again for your support.

Regards,

JD

Well a full example would be nearly my last post. And I don’t see how that could help.

Why don’t you post your project online? We could maybe debug that way…

The only issue with posting the project online is that this project is an intranet… I dont if I can do it… let me do a simple project to show you my issue, and i will post this test project online… Thank you very much! :)

Regards,

JD

I think i resolved partially my problem, now the content changes when you change page… But when i add this line on the search function, the content doesnt change


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

I tried with this Searching and sorting a column from a related table in a CGridView approach and I get the same result i cant change page and when i remove the line


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

It works but I cant search the results

I solved my problems finally =D yeeah! i made a little modification to the code is posted in this url http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview

the original code is:


return new CActiveDataProvider( 'Post', array(

    'criteria'=>$criteria,

    'sort'=>array(

        'attributes'=>array(

            'author_search'=>array(

                'asc'=>'author.username',

                'desc'=>'author.username DESC',

            ),

            '*',

        ),

    ),

));

I modified the code and using this approach i solved my issue, is almost the same but this was the only way it worked for me





$sort = new CSort;

$sort->defaultOrder = 'author_search asc';

$sort->attributes = array( 'author_search'=>array( 'asc'=>'author.username', 'desc'=>'author.username desc', ), '*', );




return new CActiveDataProvider( 'Post', array(

    'criteria'=>$criteria,'sort'=>$sort));