Yii Framework Forum: Calling save within aftersave - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Calling save within aftersave Need pk to save some items Rate Topic: -----

#1 User is offline   phazei 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 20-July 09

Posted 26 July 2011 - 05:36 PM

Can I call a models save method inside the afterSave method?

There's a directory path based on the primary key, so an uploaded file can't be saved until the primary key exists.

The only concern I have is the isNewRecord property. If I save within afterSave it won't have been set to false yet, will that make the save method attempt to do an insert instead of a delete? Also, if it isn't false it would cause a loop everytime it hits afterSave.


Would manually setting isNewRecord to false take care of all that, or is there anything else that would need to be set?
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 27 July 2011 - 01:33 AM

Never tried that...I would go with saveAttributes() after a successfull save()... and if needed use a transaction...

But in your case there is no concern with the isNewRecord... you get the primary key value before afterSave()... so you can use it to update the record... but on that update use saveAttributes() this way you prevent the calling of afterSave() again... as per documentation saveAttributes() does not call before/afterSave()

http://www.yiiframew...tributes-detail

Edit: Just noticed that Mike is right... if you dont set isNewRecord to false... saveAttributes() would add a new record... so you can set isNewRecord to false and call saveAttributes() or use updateByPk() that again does not call before/afterSave()

This post has been edited by mdomba: 27 July 2011 - 01:44 AM

Find more about me.... btw. Do you know your WAN IP?
1

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 27 July 2011 - 01:36 AM

You could set isNewRecord to false and then use $this->saveAttributes(array(...)) to only update the attributes you need. It will not call afterSave again.
1

#4 User is offline   phazei 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 20-July 09

Posted 28 July 2011 - 02:14 AM

View Postmdomba, on 27 July 2011 - 01:33 AM, said:

Never tried that...I would go with saveAttributes() after a successfull save()... and if needed use a transaction...

But in your case there is no concern with the isNewRecord... you get the primary key value before afterSave()... so you can use it to update the record... but on that update use saveAttributes() this way you prevent the calling of afterSave() again... as per documentation saveAttributes() does not call before/afterSave()

http://www.yiiframew...tributes-detail

Edit: Just noticed that Mike is right... if you dont set isNewRecord to false... saveAttributes() would add a new record... so you can set isNewRecord to false and call saveAttributes() or use updateByPk() that again does not call before/afterSave()


Ah, saveAttributes(), how'd I miss that, good to have. There were a couple places where I just used command builder to save a single attribute instead. That'll be useful.

OTOH, in this case, I used setIsNewRecord(false) in afterSave and called save again and it worked just fine. The reasoning was for new records I had the code in afterSave, but for updates I had it in beforeSave, so it was duplicated and I simply wanted to reuse the code. Of course, in actually writing it out here, I'm thinking I could have just as easily created a new method an called it in both methods, or just saved both cases in afterSave. Oh, and it is all wrapped in a transaction. First I played with starting a transaction in beforeSave and committing it in afterSave, but it was just simpler to do it in the controller. Actually, I think I'll go back and make it simpler and just use saveAttributes!

Thanks.
0

#5 User is offline   Ch3Pz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 19-October 11

Posted 25 March 2012 - 11:27 PM

View PostMike, on 27 July 2011 - 01:36 AM, said:

You could set isNewRecord to false and then use $this->saveAttributes(array(...)) to only update the attributes you need. It will not call afterSave again.


this works best for me.

Thanks
0

#6 User is offline   Jose H. Milán 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 21-July 11
  • Location:España

Posted 30 July 2012 - 04:37 PM

View PostMike, on 27 July 2011 - 01:36 AM, said:

You could set isNewRecord to false and then use $this->saveAttributes(array(...)) to only update the attributes you need. It will not call afterSave again.


Good trick, it works like a charm. Perfect for me.

Thanks
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users