How To Update Two Tables From One Form Yii

I have to update two tables from one form. I have two tables TestA and TestB. So how can I update two tables where TestB.testid=TestA.testid. Both the tables are populated. I need to update TestB based on the id of TestA. Below is the actionUpdate of TestA.


public function actionUpdate($id)

        {       $model_A=new TestA;    

                $model_B=new TestB;    

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

            if(isset($_POST['TestA'])&&isset($_POST['TestB']))

            {

                 $model_A->attributes=$_POST['TestA'];                     

                 $model_B->attributes=$_POST['TestB'];


                 $model_B->name="test";                 


                 $model_A->save();

                 $model_B->save();    

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

                'model'=>$model,

            ));

        } 

When I run the application, a new entry is created in TestB instead of updating the existing one. How can I pass the id to update the row in table TestB

Use DAO to modify second table.

Add a hidden field in the form with the id of the second model and extract it from POST to load it, just like the first one using id passed in action parameter.

Hi Rudra.

Let me understand better your problem. Correct me if I’m wrong.

You have three models: TestA, TestB and the third one comprising the attributes of both. Let us name the last one Test.

When you call


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

the Test attributes are read from both tables.

The update form of Test model contains two nested forms, one for TestA attributes, one for TestB attributes.

Let us assume that you put the testid attribute in the TestA subform. In this case


$model_A->attributes=$_POST['TestA'];

reads the form’s testid value in $model_A->testid, while


$model_B->attributes=$_POST['TestB'];

leaves $model_B->testid null. If testid of model_B is also primary key, this is why


$model_B->save();

creates new record.

You should assign manually the missing value:


$model_B->testid = $model_A->testid;

So, the result becomes:


public function actionUpdate($id)

{

	$model_A=new TestA;    

	$model_B=new TestB;    

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

	if(isset($_POST['TestA'])&&isset($_POST['TestB']))

	{

		$model_A->attributes=$_POST['TestA'];                     

		$model_B->attributes=$_POST['TestB'];

		$model_B->testid = $model_A->testid; # the previously missing assignment


		$model_B->name="test";                 


		$model_A->save();

		$model_B->save();    

	}

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

		'model'=>$model,

	));

}

Alexandre

Hi

You said TestB is an existing entry. But where do you load it? It looks as if you only create a new TestB entry and then save it. You never load it > update it > and then save it.

Am I mistaken?

just try it…




$TestA=$this->loadModel($id, 'TestA');

$TestB=$this->loadModel($TestA->testa_id, 'TestB');

and


print_r($TestB->Attributes);

i hope it’s work…

Try this







public function actionUpdate($id)

{

    //load A by id

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

    //find B by $testid from A id

    $model_B=$this->loadModelB($model_A->testid);

    if(isset($_POST['TestA'])&&isset($_POST['TestB']))

    {

     	$model_A->attributes=$_POST['TestA'];                 	

     	$model_B->attributes=$_POST['TestB'];


     	$model_B->name="test";             	


     	$model_A->save();

     	$model_B->save();

    }

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

        'modelA'=>$model_A,

        'modelB'=>$model_B,

    ));

}