Yii2: ActiveRecord::deleteAll([ ... ]); not triggers events

Created on 3 Dec 2013  路  6Comments  路  Source: yiisoft/yii2

Method \yii\db\ActiveRecord::deleteAll([ ... ]); not triggers events
ActiveRecord::EVENT_AFTER_DELETE
ActiveRecord::EVENT_BEFORE_DELETE

Most helpful comment

@qiangxue

a more efficient way than foreach and Model::delete() is to find all models before the delete, call deleteAll() and trigger the events.

This causes just two Queries: SELECT and DELETE on the condition. The foreach and Model::delete() does a separate DELETE for each model which is a lot of overhead.

My code looks like this:

$models = Model::find()->where($condition)->all();

foreach ($models as $model) {
    $model->beforeDelete();
}

Model::deleteAll($condition);

foreach ($models as $model) {
    $model->afterDelete();
}

All 6 comments

This is by design because there is no AR instance being deleted. deleteAll() is equivalent to DELETE FROM table.

For simple removal fields from the table, I'd use a

(new Query())
    ->createCommand()
    ->delete('table', 'status=3');

Using ActiveRecord I assumed that includes all the dependencies between data in different tables.

Well, that is by design, which gives much better performance and allows you to delete rows using AR class name instead of table name.

If you really want to trigger those events, you may use the following code:

foreach (User::find()->where('status=3')->all() as $user) {
    $user->delete();
}

I usually do something like that User::deleteAll(
['user_right_id' => $user_id, 'user_has' => $user_has]
)
And you?

ControllerName::deleteAll(['user_id' => \Yii::$app->user->identity->id]);
this line of code saved my day

@qiangxue

a more efficient way than foreach and Model::delete() is to find all models before the delete, call deleteAll() and trigger the events.

This causes just two Queries: SELECT and DELETE on the condition. The foreach and Model::delete() does a separate DELETE for each model which is a lot of overhead.

My code looks like this:

$models = Model::find()->where($condition)->all();

foreach ($models as $model) {
    $model->beforeDelete();
}

Model::deleteAll($condition);

foreach ($models as $model) {
    $model->afterDelete();
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

MUTOgen picture MUTOgen  路  3Comments

AstRonin picture AstRonin  路  3Comments

psfpro picture psfpro  路  3Comments

kminooie picture kminooie  路  3Comments

Kolyunya picture Kolyunya  路  3Comments