Rudra
Rudra

Reputation: 739

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

Upvotes: 0

Views: 2769

Answers (3)

davey
davey

Reputation: 1791

Assuming you're in controller for Model_A and you're only updating Model_A (you're in the update action).

  • First load model A
  • Fill the attributes of model A with POST data
  • Load model B, if not exists, create new model B
  • Fill the attributes of model B with POST data
  • Save model A and model B

Though it's nicer (and easier) to use relations here and put the whole model B stuff in the afterSave method of model A.

 public function actionUpdate($id)
        {
            $model=$this->loadModel($id);

            if(isset($_POST['TestA'])&&isset($_POST['TestB']))
            {
                 $model->attributes=$_POST['TestA'];

                 $modelB = TestB::model()->findByAttributes(array('testid'=>$model->testid));

                 if ($modelB == null) {
                   $modelB = new TestB;
                   $modelB->testid = $model->testid;
                 }

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


                 if ($model_A->save()) {
                    $model_B->save();
                 }

                $this->render('update',array(
                    'model'=>$model,
                ));
        }

Upvotes: 0

Pitchinnate
Pitchinnate

Reputation: 7566

Ok if this is update you need to first pull the existing values from the database, then you need to make sure you send both models to the form:

public function actionUpdate($id) {
    $model_A = TestA::model()->findByPk($id);
    $model_B = TestB::model()->findByAttributes(array('testid'=>$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(
        'model_A' => $model_A,
        'model_B' => $model_B,
    ));
}

Upvotes: 2

davey
davey

Reputation: 1791

You can access all model properties (including the ID) after you saved it. So after you saved $model_A, $model_A->testid will contain the testid of the just saved model (model A).

if ($model_A->save()) {
   $model_B->testid = $model_A->testid;
   $model_B->save();
}

Upvotes: 0

Related Questions