1

I have two tables table1 and table2 and I am trying to delete 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 actionDelete($id)
 {
 $this->findModel($id);
 $select = Employee::find()
            ->select('Name')
            ->where(['Id' => $id])
            ->all();
$deluser=Employee::find()->where(['Id' => $id])->one()->delete() AND User::find()->where(['Name' =>$select])->one();
$deluser->delete();
 return $this->redirect(['index','select'=>$select]);
}  

pls anyone help me Thanks in advance

1
  • pls put it back, what you have change in your question Commented Jun 16, 2016 at 5:42

1 Answer 1

3

Employee class is object.

Try this:

public function actionDelete($id)
{
    $this->findModel($id);
    $select = Employee::find()
                ->select('Name')
                ->where(['Id' => $id])
                ->all();

    Employee::find()->where(['id' => $id])->one()->delete();

    User::find()->where(['id' =>$id])->one()->delete();

    return $this->redirect(['index','select'=>$select]);
}  
Sign up to request clarification or add additional context in comments.

12 Comments

I tried this way but PHP Fatal Error – yii\base\ErrorException Call to a member function delete() on a non-object getting this error
both table have different id value so i can't delete using $id in user table. But both table have column Name and same value so , how to find name in employee table using $id and based on that name ,delete row in the user table.
Why name? why not use some kind of relation, like put user id into employee table as foreign key?
ok i created employee id in user table as foreign key instead of you said then how to change my delete query?
what is field name in user table? User::find()->where(['employee_id' =>$id])->one()->delete();
|

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.