Reputation: 55
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,Status
enum('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
Upvotes: 0
Views: 2475
Reputation: 9
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,
]);
}
Upvotes: 0
Reputation: 5731
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,]);
}
}
Upvotes: 0