Open a761208 opened 6 years ago
Woah, this one will not be an easy one :)
@a761208 in case you have an idea how to fix it – we will be happy to see a Pull Request from you
I was facing the same problem, but ended up setting pk manually to null. I have a idea now, I will try to submit a pull request soon.
@berosoboy NOT just insert and not just ActiveRecord. Every thing changed in the trans should be reset after rollback.
The model should be used to populate the form input controls. So, although transaction had thrown an exception, the model should contain all attributes, also that changed by request (post or get). The only thing to reset is the pk (in catch exception block), that in create action is never read from input. So even if the primary key remained set, this should not be a problem.
Every thing changed in the trans should be reset after rollback.
@a761208 It could lead to undesirable behavior. The programmer needs to be responsible to handle it.
I agree with you @FabrizioCaldarelli . My PR, (work in progress) targets only the pk reset and, most important, the $model->isNewRecord
reset. (this last made me crazy once :D , because sometimes I trust the $model->isNewRecord
in my view files)
The only thing to reset is the pk (in catch exception block), that in create action is never read from input.
That is an incorrect assumption for several reasons:
I think this can be partially solved using the event system (ie registering the model to revert its attributes on the ROLLBACK event). However there are still scenarios where behavior will be unexpected.
afterSave()
; Yii provides isTransactional
allowing me to make the afterSave()
handler part of the transaction.afterSave()
; the intuitive assumption is that a record has been saved during afterSave()
, but if the transaction is rolled back this is not the case.Not sure if there's 1 solution that fits both of these scenarios.
What steps will reproduce the problem?
What is the expected result?
Empty $model->id
What do you get instead?
There is a value in $model->id
Additional info