MaxCfasman
(Max Levinson)
February 19, 2010, 8:13pm
1
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.
mbi
(mbi)
February 19, 2010, 8:53pm
2
$this->render('create', compact('view_array'))
tri
(tri - Tommy Riboe)
February 19, 2010, 9:00pm
3
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.
I think you should expect to have a bunch of $faked_model# objects in the view (not $view_array).
/Tommy
MaxCfasman
(Max Levinson)
February 19, 2010, 10:27pm
4
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,
));
tri
(tri - Tommy Riboe)
February 20, 2010, 1:31am
6
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.
Your original code should work, just access the generated models in the view e.g.
echo $faked_model1->some_member;
/Tommy