How to pass multiple models to view.

Hi guys,

Having another quite sophisticated question about Yii. I need to pass close to 40 model(form) instances to view.

Look what I do:

First of all I am counting amount of instances which I am trying to pass from controller to view:




$amount_of_extensions = file_extensions::model()->count('"file_set_type"=:file_set_type',array(':file_set_type'=>1));



Then I instantiate all those models which I need dynamically:




for ($i=1;$i<=$amount_of_extensions;$i++)

{

   ${'faked_model'.$i} = new faked_model;

} 



Then I need to add those variables to associative array which I need to pass to view:




for ($j=1;$j<=$amount_of_extensions;$j++)

{

  # push data to assoc array

  $view_array['faked_model'.$j]= ${'faked_model'.$j};

}



Then I do this(passing actual associative array to view) which should work:

$this->renderPartial(‘create’,$view_array);

I am getting an error in view which is saying:

Undefined variable: view_array

I am just wondering what I am doing wrong.


$this->render('create', compact('view_array'))

I think you should expect to have a bunch of $faked_model# objects in the view (not $view_array).

/Tommy

Hi Tommy.

I haven’t completely got what you mean, but renderPartial requires associative array as a second parameter. I looked here:

http://www.yiiframework.com/doc/api/CController#renderPartial-detail

Ideally I need something like this:




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


 'model'=>$model,

 'faked_model1'=>$faked_model1,

 ........

 'faked_model40'=>$faked_model40,


));



But my problem is I need a way how to add this dynamically, because amount of models which I need to pass is changing dynamically. Hope it will give you an idea of what I want and you will be able to help me, I know you can.

Thanks for your time.

yes

look this http://www.yiiframework.com/doc/guide/form.table

and try with




faked_model_all=array();

faked_model_all[]=$faked_model1;

 ........

faked_model_all[]=$faked_model40;


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


 'model'=>$model,

 'faked_model_all'=>$faked_model_all,


));



Your original code should work, just access the generated models in the view e.g.




echo $faked_model1->some_member;



/Tommy