yii2: AfterSave can not rollback saving transaction
What steps will reproduce the problem?
- Implement
afterSave()to save any sub-models depending of the primary key of the main model - return
falseto afterSave() when saveing of ony of the sub-models fails
What is the expected result?
The transaction of the main model should be rolled back without throwing an exception. (when using transactions).
And should return save() of BaseActiveRecord should return false when not using transactions.
What do you get instead?
transaction is not rolled back. and BaseActiveRecord::save() returns true
Additional info
There was already an similar issue #8392 and a comment suggesting a change that would fix the problem see https://github.com/yiisoft/yii2/issues/609#issuecomment-20873800
Suggestion how to fix
change the call of afterSafe()-call in ActiveRecord::insertInternal and BaseActiveRecord::updateInternal to return its result e.g. like this:
protected function insertInternal($attributes = null)
// .... other code
if (!$this->beforeSave(true, $changedAttributes)) {
return false;
}
}
UPDATE This ensures that only when returnung bool false the saving is interupted
protected function insertInternal($attributes = null)
// .... other code
if ($this->beforeSave(true, $changedAttributes) === false) {
return false;
}
}
why other ideas to solve this won`t work
- save or validate the related models in beforeSave()
- won`t work, because they need the saved primaryKey of the main model
- overwriting save() methode and add an own transaction that can be rolled back when save of submodels fails
- this works, but it is a bad way because needs a duplication of the most try/catch code from
ActiveRecord::insert
- this works, but it is a bad way because needs a duplication of the most try/catch code from
- throw an exception within afterSave() like suggested in https://github.com/yiisoft/yii2/issues/8392#issuecomment-101576185
-
- this would result in an exception thrown by the save method instead of returning
false
- this would result in an exception thrown by the save method instead of returning
-
- it is a inconsistend behavior that
beforeSave()can rollback the save with returning false, andafterSave()must throw
- it is a inconsistend behavior that
-
- method signature of
afterSave()does not say that it could throw exceptions, what could lead to unexpected behavior if you throw exceptions there
- method signature of
-
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 19 (10 by maintainers)
This will change how method works and in some way changes its signature - for 10 years
afterSave()was expected to return nothing and all code works like this. Changing this expectation may result errors in existing code. Also, this makes sense only if you start a transaction (and rollback on error), which Yii does not automatically (and IMO should not). So havingafterSave()that returnbooland ignore this value by framework does not look right from design perspective.I really do not understand what is the problem with throwing an exception in
afterSave()and overwritingsave()to catch these exceptions and handle transaction. This really looks like straightforward solution that requires like 10 lines of code (that could be extracted to trait or base class).AfterSave was clearly designed to not changed the result of
insertInternal()andupdateInternal(). As this is purely a matter of creators’ decision I can understand that some people might see it differently than planned. Even though this could be changed, it’s BC breaking change, so not in Yii 2.I think that says it all.
If you want to make such changes to the basic methods of the framework, you have to write a custom ActiveRecord class and extend your models from it, or implement one of the proposed “solutions” mentioned here or in https://github.com/yiisoft/yii2/issues/8392.
In the framework itself, however, such signature changes within yii2 are not (or no longer) an option.