saveAttributes() in afterSave()


I tried to call saveAttributes() in the afterSave() callback to update a couple of attribute values of my model and I got this exception - "The active record cannot be updated because it is new".

The issue is the same as one discussed here -

Qiang’s suggested workaround for this was to manually set isNewRecord to ‘false’, save/update attribute value (and set isNewRecord back to ‘true’ if necessary) inside afterSave(). Are there any other ways to do this?


saveAttributes() uses updateByPk() method. Maybe you’ll find it useful…

That’s how i solved that, too. Why don’t you like it?

I ended up solving it this way too but I was hesitant because of the same reason that the person who posted the issue was - that I had to manually set the ‘isNewRecord’ flag to false, then to true in order to save an attribute in a new record. I understand the reasons for the design but was only checking with the community to see if subsequent framework releases provided a different way to solve it that I might have missed.

Sorry for the delayed response - I forgot to update my notifications and saw this just now.