Event on updateAttributes


(adan) #1

Hi:
I have a code (2amigos user module) that updates attributes with “updateAttributes”, so, although I override the model, my afterSave function is not called after “updateAttributes”.

Is there any way to do something in my overrided User model (“above” his User model) to execute some code after the “updateAttributes”?

I’ve tried with Events, but no lucky:

    public function init()
    {
        parent::init();
        Event::on(\yii\db\ActiveRecord::className(), \yii\db\ActiveRecord::EVENT_AFTER_UPDATE, function ($event) {
            $this->afterSave(null, $event->changedAttributes);
        });
    }

P.D. Obviously, I try to do something like that because I can not change the “vendor” code :wink:


(stitch) #2

https://www.yiiframework.com/doc/api/2.0/yii-db-baseactiverecord#updateAttributes()-detail
** Note that this method will not perform data validation and will not trigger events.**
Check your overrided afterSave method, that you call parent::afterSave()
When overriding this method, make sure you call the parent implementation so that the event is triggered.


(adan) #3

I think I don’t understand the answer or I didn’t explain myself clearly.

What I need is to execute something after a “updateAttributes” (already written in a vendor class) is executed.

I can not override its functions, it would be so many changes that I’d open my own branch of the project, but I don’t want to be out of date in future updates.

So what I’m looking is to create an event that triggers after the “updateAttributes” of a model.

Is that possible?


(adan) #5

what !? I don’t understand your post and suggestion of deletion :frowning: