0

I want to delete updated value from db. Like if the notice is inserted of notice column in class table and I want to delete it. So what will be the query in yii2? Please help -

$model = Class::find()->where('id', $id)->one;
$model->delete();

Above query is for -

DELETE FROM class WHERE id = $id;

I want to write query like that in yii2 -

DELETE notice FROM class WHERE id = $id;

2 Answers 2

1

If you are searching using the primary key, you can use

$model = Class::findOne($id);

Then, for deleting (setting to null) the notice field, you just need to use

$model->notice = NULL;
$model->save();

Note that if the notice attribute is required, or not null, the save will fail.

Sign up to request clarification or add additional context in comments.

Comments

0

Yii allows you to execute SQL command direct https://www.yiiframework.com/doc/api/2.0/yii-db-command

$query = "DELETE notice FROM class WHERE id = {$id}";
Yii::$app->db->createCommand($query)->execute();

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.