简体   繁体   中英

Yii2 updateAll() not triggering afterSave()

I have implemented some logic in afterSave method on every insert or update.

But afterSave is not executed where I have used updateAll().

So is there any alternative for the same ?

afterSave() is thought for a single record. updateAll() handles many records in a completly different way. So you cannot use afterSave() this way. Imagine: when you update thousands of records this way, the afterSave() methods would need to be executed for each record - you would probably need something like afterSaveAll() that gets called once and that does only a single (or some few) further update, right?

When you call ActiveRecord::updateAll() and you want that something happens afterwards (for all updated records) you have to do it explicitly after that call. Or you might could modify what you're doing with updateAll(). Otherwise you have to tell us in more detail what you try to achieve.

Or as Bizley has commented: if you you know that you are only updating a couple of records you could do it in a loop. So you would not use updateAll() , rather you update each record separatly, with a call of save() and the implicit call of afterSave() that you have already implemented. You could give it a try, it might be acceptable from an performance perspective.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM