Method \yii\db\ActiveRecord::deleteAll([ ... ]); not triggers events
ActiveRecord::EVENT_AFTER_DELETE
ActiveRecord::EVENT_BEFORE_DELETE
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();
}
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: