AnsweredAssumed Answered

How do I best order by before_save and after_save hooks?

Question asked by Gaelle Fernandez on Jul 4, 2014
Latest reply on Jun 9, 2015 by sonesay inthavong
Hello everyone!

I have a little Logic issue, and i would like your point of view on this :

In my application, i have the meetings module.
When i create a meeting, i always have to udpate its subject/name.
When a meeting's status comes to "Held" AND the value "Meeting" is chosen under "next_action_c" dropdown, I have to create another meeting, children of the current/first one. (and populate some fields from the first one, like $meeting2->parent_meeting_id = $meeting2->id)

In Meetings Modules, i call two function : 

** Update Subject, which updates my meeting subject
** Creating New Meeting, which creates new meeting, children of the current meeting.

The first function is called in a before_save hook.
The second is called in an after_save hook. (so, after getting $newMeeting = new Meeting(); and populate it, i have to call $newMeeting->save() )

So far, everything goes well.

EXCEPT, i want to update the subject of second meeting differently from the first! So I would like my first function, updateSubject() to run only for a fresh new Meeting. That is why i test if the Meeting i'm saving does have a parent_meeting_id.

BUT, and i've tested it, when creating the second one, because the updateSubject is running BEFORE SAVE, the parent_meeting_id is not filled, because it comes with creatingNewMeeting : AFTER SAVE, so the updateSubject is still running and filling my subject the wrong way...

I'm sorry, i can't be more precise, my explanations are a bit rough, but i've practise it on somebody else...

Could you please help me on that logic issue, please?

Thanks a lot,