How to create a multi-model form in Yii? I searched the entire documentation of Yii, but got no interesting results. Can some one give me some direction or thoughts about that? Any help will be appreciable.
4 Answers
In my expirience i got this solution to work and quickly understandable
You have two models for data you wish collect. Let's say Person
and Vehicle
.
Step 1 : Set up controller for entering form
In your controller create model objects:
public function actionCreate() {
$Person = new Person;
$Vehicle = new Vehicle;
//.. see step nr.3
$this->render('create',array(
'Person'=>$Person,
'Vehicle'=>$Vehicle)
);
}
Step 2 : Write your view file
//..define form
echo CHtml::activeTextField($Person,'name');
echo CHtml::activeTextField($Person,'address');
// other fields..
echo CHtml::activeTextField($Vehicle,'type');
echo CHtml::activeTextField($Vehicle,'number');
//..enter other fields and end form
put some labels and design in your view ;)
Step 3 : Write controller on $_POST
action
and now go back to your controller and write funcionality for POST action
if (isset($_POST['Person']) && isset($_POST['Vehicle'])) {
$Person = $_POST['Person']; //dont forget to sanitize values
$Vehicle = $_POST['Vehicle']; //dont forget to sanitize values
/*
Do $Person->save() and $Vehicle->save() separately
OR
use Transaction module to save both (or save none on error)
http://www.yiiframework.com/doc/guide/1.1/en/database.dao#using-transactions
*/
}
else {
Yii::app()->user->setFlash('error','You must enter both data for Person and Vehicle');
// or just skip `else` block and put some form error box in the view file
}

- 2,134
- 17
- 28
-
I know it can works. But I'm not sure if it's hurts the MVC pattern. Or else wouldn't exists CFormModel. AFAIK it's 1 Model <> 1 Controller <> n Views – armandomiani Jul 17 '11 at 09:45
-
I think it doesnt break anything for the structure. But for more examples i think maybe one more solution would be ok: Create new model which merges data/columns from two models. And now use FormModel in view as one model `CHtml::activeTextField( $MergedModel , 'type');`. But this solution is more confusing and ugly.. hmm – briiC Jul 18 '11 at 07:23
-
This is the methodology presented in the yii manual, but imo is clearer to understand, http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/ – John Zumbrum Jun 17 '12 at 15:19
You can find some examples in these two Yii wiki articles:
You don`t need a multi-model. The right use of the MVC pattern requires a Model that reflects your UI.
To solve it, you'll have to use a CFormModel instead of an ActiveRecord to pass the data from View to Controller. Then inside your Controller you`ll parse the model, the CFormModel one, and use the ActiveRecord classes (more than one) to save in database.
Forms Overview and Form Model chapters in Yii Definitive Guide contains some details and samples.

- 17,148
- 27
- 124
- 216

- 732
- 6
- 16
-
`The right use of the MVC pattern requires a Model that reflects your UI` - whoever taught you this was fatally wrong – rostamn739 Nov 30 '15 at 16:45
Another suggestions -
Also we can use Wizard Behavior, It's an extension that simplifies the handling of multi-step forms. In which we can use multi model forms for registration process flow or others.

- 3,528
- 1
- 42
- 48