how to update multiple table from one action

Hello

I am new in Yii framework. i want to update 2 tables from one action.

for this i create 2 different models instants. but we i am using loadModel function it give error.

Please help me to do that.

Maybe post your code and someone will definitely take a look. You can update two tables from one action, if you have two models already created here’s something you can do:




public function actionUpdate2Tables()

{

 if (isset($_POST['Model1']))

  $model1->save();


 if (isset($_POST['Model2']))

  $model2->save();


} 



A general code, maybe not very helpful, just an example however.

Here some docs and articles on the subject.

How to use a single form to collect data for two or more models?

http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/

Handling Related Models in Yii Forms

Hi Thanks for your reply

But as i told you that i am using loadModel function so that my fields will fill out fill out correctly.

so my problem is that when i am using loadModel for model(A) then i am using loadModel for model(B)

it gives me error. so i created one more replica of loadModel function that is loadModel1().

but it converts model(B) to model(A). I dont know how.

Here’s how you can have your own loadModel:

$model1 = $this->loadModel($id);

For Model 2 , external model:

$model2 = Model2::model()->findByPk($id);

$model2->attribute = ‘update value?’;

$model2->save();

Hope this example helps you.

Thanks its working.

does not working for me

my controller is

public function actionUpdate($id)

{


	$model=$this->loadModel($id);





	// Uncomment the following line if AJAX validation is needed


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





	if(isset($_POST['Employees']))


	{


	    $model2=new Staffrole;


		$model->attributes=$_POST['Employees'];


		$model2->attributes=$_POST['Staffrole'];


		$model2 = model2::model()->findByPk($id);





        $model2->attribute = 'update value?';





        $model2->save();


		if($model->save())


			$this->redirect(array('view','id'=>$model->empid));


	}





	$this->render('update',array(


		'model'=>$model,


	));


}