Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Update After Save (Infinitive Loop)

$
0
0
Hi

I have a model related by another one

the schema's are:

model1: id, default_model2_id (null is permitted)
model2: id, model1_id (is required!)

I want to save them but there is a problem

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!

Thanks :)

Viewing all articles
Browse latest Browse all 18717

Trending Articles