If the first model when it is new record, there is not id yet to save the second model.
So in this case I save the first model on beforeSave and on afterSave I save the second one and update the first one
pseudocode
beforeSave() {
-save the first
}
afterSave() {
-save the second (related with the first model id -> now the id exists!)
-update the first one (related with the second model, id just created on the previous line
}
The problem is the update function invokes the beforeSave again, causing infinite loop!
Does anyone has a robust way to solve this problem?
I could set a flag on the afterSave method to prevent the beforeSave again but I want a better aproach!