0

I have two tables table1 and table2 and I am trying to update row in these two table.I have same values on both table but id is different so i tried like this, my controller,

public function actionUpdate($id)
  {
   $model = $this->findModel($id);
   if ($model->load(Yii::$app->request->post()) && $model-     >validate()) {
         Employee::find()->where(['Id' => $id])->one()->update();
         User::find()->where(['User_id' =>$id])->one()->update();
         if ( $model->save()) { 
        return $this->redirect(['index']);
         }
    } else {
        return $this->render('update', [
            'model' => $model,]);
    }
}

I have table like this

CREATE TABLE Employee ( Id int(11) NOT NULL,Company_company_id int(100) NOT NULL,Company_name varchar(100) NOT NULL, Employee_id int(100) NOT NULL, Name varchar(100) NOT NULL,Email_id varchar(100) NOT NULL,Password varchar(16) NOT NULL,Joining_date date NOT NULL,Confirmation_date date NOT NULL, Relieving_date date NOT NULL,Leaves_Available int(25) NOTNULL,Statusenum('Active','Inactive') NOT NULL,)

CREATE TABLE User (Id int(15) NOT NULL, Name varchar(100) NOT NULL,Email_id varchar(100) NOT NULL,Password varchar(16) NOT NULL,Status enum('Active','Inactive') NOT NULL,)

I tried like this but i can't update both table
pls anyone help me Thanks in advance

3
  • What is error message??? Commented Jun 16, 2016 at 6:03
  • pls post your both model's name. Commented Jun 16, 2016 at 6:05
  • Employee() and User() are my models. Commented Jun 16, 2016 at 6:07

2 Answers 2

0

Try This :

public function actionUpdate($id)
  {

   if ($model->load(Yii::$app->request->post()) && $model->validate()) 
   {
        $modelEmp= Employee::find()->where(['Id' => $id])->one();
        $modelUser= User::find()->where(['User_id' =>$id])->one();

        $modelEmp->Name=$_POST['name']; // use your field names
        $modelEmp->Email_id=$_POST['email_id'];

        $modelUser->Name=$_POST['name'];
        $modelUser->Email_id=$_POST['email_id'];

         if ( $modelEmp->save() && $modelUser->save()) { 
            return $this->redirect(['index']);
         }
    }
    else {
        return $this->render('update', [
            'model' => $model,]);
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I added with my question.
I tried like this but i am getting error, PHP Notice – yii\base\ErrorException Undefined index: name
use your field names as you use in your form
<?= $form->field($model, 'Name')->textInput(['options' =>['name' =>'name','id' =>'name']]) ?> <?= $form->field($model, 'Email_id')->input('email',['options' =>['name' =>'email_id','id' =>'email_id']]) ?> I gave like this .
try to check your POST data in your controller
|
0

Here is simple example how I made savings to different tables from one controller.

    public function actionCreatesolo()
{
    $model = new Productordergroup();
    if ($model->load(Yii::$app->request->post()) && $model->save()) {

        $id = $model['product_id'];  //get selected product id
        $product = Product::findOne($id); //find product by selected id
        $product->grouped = '1';  //update value "groped" from 0 to 1
        $product->save(); //Save changes
        return $this->redirect(['view', 'id' => $model->id]); 
    }

    return $this->render('createsolo', [
        'model' => $model,
    ]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.