how to update multiple table from one action


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']))


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



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?

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?’;


Hope this example helps you.

Thanks its working.

does not working for me

my controller is

public function actionUpdate($id)



	// Uncomment the following line if AJAX validation is needed

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



	    $model2=new Staffrole;



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

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