Inserting Data In 2 Tables Using Same Form

hi,

i have two tables. one it_device (sbitid, sbequip,sbbrand, sbmodel, sbserial, sbpurchsdt, sbcurrentuser, sbhandovrdt) and eqiplogs (id, currentuser, date). whenever i insert\update data into it_device it should insert one row in eqiplogs with id, currentuser and date information. i tried to follow wiki but it didn’t work out. it updates data into it_device and not in equiplogs.

my code is mentioned below:

<public function actionCreate()

{


	&#036;model=new ItDevice;


	&#036;model2=new Euiplogs;





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['Euiplogs']))


	{


		&#036;model2-&gt;attributes=&#036;_POST['Euiplogs'];


		if(&#036;model2-&gt;save())


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model2-&gt;sbitid));


	}


	


	if(isset(&#036;_POST['ItDevice'])){


		&#036;model-&gt;attributes=&#036;_POST['ItDevice'];


		if(&#036;model-&gt;save())


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;sbitid));


	}





	&#036;this-&gt;render('create',array(


		'model'=&gt;&#036;model,


		'model2'=&gt;&#036;model2,


	));


}

>

Could you please help me in this case?

because you are checking the $_POST array twice separately 1st for the presence of Euiplogs and next for ItDevice if you POSTED Euiplogs and ItDevice the first if statement gets executed, saves Euiplogs to the table and then you instruct the YII to redirect the to the last inserted record’s view page so the next condition will be skipped so that’s is a problem so can remove the redirect line from the first condition or rewrite the entire method

thanks for quick reply. But i have tried both the methods. in both cases its saving only in ItDevice table and not in another one.

I would like to rewrite your code :)

Just trylike this… not tested

public function actionCreate()

{

 &#036;model=new ItDevice;


 &#036;model2=new Euiplogs;





 // Uncomment the following line if AJAX validation is needed


// &#036;this-&gt;performAjaxValidation(&#036;model);





if(isset(&#036;_POST['Euiplogs'])) [size=2]{[/size]


       &#036;model2-&gt;attributes=&#036;_POST['Euiplogs'];


       if(&#036;model2-&gt;save()){


           //&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model2-&gt;sbitid));





          if(isset(&#036;_POST['ItDevice'])){


              &#036;model-&gt;attributes=&#036;_POST['ItDevice'];


              if(&#036;model-&gt;save()){





                  &#036;this-&gt;render('create',array(


                    'model'=&gt;&#036;model,


                     'model2'=&gt;&#036;model2,


                 ));


       //&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;sbitid));


}

} else{

$this->render(‘create’,array(

‘model’=>$model,

‘model2’=>$model2,

));

}

}

[size=2]}[/size]