Multiple Model With Multiple Cgridview


Is There easy way to have multiple same type models and show these in multiple cgridview (for each model) ?

For example

$model1 = new MyModel('search');

$model2 = new MyModel('search');



 $this->widget('CGridview', array(

        'id' => 'id-1',

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

        'filter' => $model1,

        'columns' => array(...)...)

 $this->widget('CGridview', array(

        'id' => 'id-2',

        'dataProvider' => $model2->search2(),

        'filter' => $model2,

        'columns' => array(...)...)

The problem is that there is confusing on ajax requests between 2 cgridview, how to solve that?


Does explicitly setting the ajaxVar property help?

Hi Keith, thanks for your response

Unfortunately it not works…

Both of two Cgridview send back to the server the same form data name variables

I check with network firebug that sends MyModel[column1], MyModel[column2] …

Is there cgridview property that sent alias of this models (for example MyModel1, MyModel2) ?

In this way I could manipulate the POST form data in controller…

How to do that ?

Okay, here’s the best I can come up with…

In your view, before outputting the first CGridView:

CHtml::setModelNameConverter(function($model){return 'MyModel1'});

Then, in between the two CGridViews:

CHtml::setModelNameConverter(function($model){return 'MyModel2'});

Then, after the second CGridView:


You would then need to update the controller to populate the two models using the correct array.

I can’t test it because I use Yii ver 1.1.13 and this method exists since 1.1.14.

I have compatibilities issues with 1.1.14 in my project.

So I will test it on the next project, In any way thanks!

Well, a hacky way to do it would be to put something like the following directly after your controller class:

class MyModel2 extends MyModel



You could use that to differentiate the request data. Not very pleasant though…

What are the incompatibilities you’re experiencing?

Although this trick way helps me to solve the problem I avoid it because I try to keep my code simple and readable, so the previous way is the best (and voted :) I will try to make my project compatible with ver 1.1.14. and I will use your tips

I think also setModelNameConverter added in new version because few user (included me) asked solution for similar issues in the past.

So thanks a lot the Yii developer team :)