afterDelete not called in deleteByPk, deleteAll

By accident I discovered that afterDelete (and beforeDelete) are not called when you delete a record using deleteByPk or deleteAll.

Looking at the code for these two methods, it’s obvious why this doesn’t happen (they construct queries and execute them rather than load models). My question, however, is whether this is correct. I personally expected it to and the manual doesn’t say it doesn’t.

I faced the same problem just now. I’d expected deleteAll() to work similar to delete() one and interested in why it doesn’t. If the reason is performance caused by loading models and separate deleting them then deleting using models could be optional. Otherwise I have to think of workaround.

How to manage a before or after delete for function like deleteAll or deleteByAttributes ??

You can’t really, given that Yii is just generating a command for the database to execute. If you need to use your afterDelete() method, you’ll probably have to use one of the find() methods to retrieve all of the rows as active record objects, then call delete() on each of them.

If performance is an issue, you could write your own custom code to do whatever you need to after bulk deletion.