NewUser
(Ajay 0162)
February 9, 2012, 7:29am
1
Hello All,
Is it possible to use more than two models in a [url="http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/"]multimodel[/url] form. If yes then why my third models data is not saving in its database?or is there any way to use it.
l.jurgs
(L Jurgs)
February 9, 2012, 8:00am
2
You can have as many models as you want but you are going to need to pull the data out of POST or GET and perform validation and saving.
<?php echo CHtml::errorSummary(array($a,$b,$c)); ?>
<div class="row">
<?php echo $form->labelEx($a,'a_field'); ?>
<?php echo $form->textField($a,'a_field'); ?>
<?php echo $form->error($a,'a_field'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($b,'b_field'); ?>
<?php echo $form->textField($b,'b_field'); ?>
<?php echo $form->error($b,'b_field'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($c,'c_field'); ?>
<?php echo $form->textField($c,'c_field'); ?>
<?php echo $form->error($c,'c_field'); ?>
</div>
public function actionCreate()
{
$a=new A;
$b=new B;
$c=new C;
if(isset($_POST['A'], $_POST['B'], $_POST['B']))
{
// populate input data to $a and $b
$a->attributes=$_POST['A'];
$b->attributes=$_POST['B'];
$c->attributes=$_POST['C'];
// validate BOTH $a, $b and $c
$valid=$a->validate();
$valid=$b->validate() && $valid;
$valid=$c->validate() && $valid;
if($valid)
{
// use false parameter to disable validation
$a->save(false);
$b->save(false);
$c->save(false);
// ...redirect to another page
}
}
$this->render('create', array(
'a'=>$a,
'b'=>$b,
'c'=>$c,
));
}
Please note that I just edited the code on the wiki page.