single form to collect data for two or more models


i want to have a single form to collect data for two models. I already viewed the documentation ‘How to use a single form to collect data for two or more models?’. I have incoperated it in my code but its not working.

The following is my controller code

public function actionCreate()


	$model=new BusType;

     $model2=new BusProducts;

	// Uncomment the following line if AJAX validation is needed

	// $this->performAjaxValidation($model);







				$model2->bus_product_id = $model->bus_type_id;  









I also made the changes in _form i.e echo $form->errorSummary(array($model,$model2)); and the changes in create i.e $this->renderPartial(’_form’, array(‘model’=>$model,‘model2’=>$model2));.

But the code is still not working. Do i need to make changes anywhere else?

Any help is appreciated. Thanks

You didn’t save your $model2(BusProduct) in your code. It might be the cause, I guess.

I tried that. Is working now. Thanks softark.

I have another issue related to the same. I want to make some inserts in model2. Currently i am able to isert the FK. How do i insert other data? Am i supposedd to use aftersave() for the same? If so can u please tell me how to go about that? Thanks

hi mithila,

here i am using ChargesDemandMaster to be automatically added to chargesDemandDetails

I have written this code in ChargesDemandMaster controller so that data would automatically get inserted in chargesDemandDetails.

i hope it will work for u.

protected function afterSave()


              $profile = new ChargesDemandDetails;

              $profile->Demand_type= $this->DemandType;

              $profile->Demand_period = $a[$i];

              $profile->Demand_flat_number = '204';

              $profile->Demand_gen_Date = $this->Demand_effective_date;

              $profile->Demand_amount =$this->Demand_amount;

              $profile->Demand_paid_flag = 'NO';

              $profile->Demand_collected_amount =  '0000';

              $profile->Demand_collected_revenueID = '0000';

              $profile->Demand_collectedDate = $this->Demand_effective_date;

               $profile->Demand_due =  $this->Demand_effective_date;

               if (!$profile->save()) {

                                             echo "didn't work";

                                               Yii::app()->end(); // don't use die();




Thanks Rama.

How to insert an array of data. I know i am supposed to use foreach but how do i go about it? Any help would be appreciated. Thanks

An array of data? you mean several models or a model attribute with several data? I dont understand your question exactly.

hi mithila,

If you newbie just like I am, I recommended you multimodelform extension


what i meant was a model attribute with several data.

The thing is basically when i submit a form i want the inserts to go in 2 different tables.

If u have read the description of my models, what i want is in the 2nd table ‘multiple’ entries of subcategories for every category. How do i go about this?