dimanche 26 juin 2016

yii2: Update values in two tables using single controller action?

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

Aucun commentaire:

Enregistrer un commentaire