As the title says.
I have an action where multiple models are used and data is stored in everyone of them.
It’s something like this:
then assign attributes to $model2
then assign attributes to $model3
Everything is working, but if the 2nd or the 3rd model fails lets say because some data is missing, the first model will be saved and the rest won’t.
Is there any way to do a roll back if any of them failed?
In your action:
$transaction = Yii::app()->db->beginTransaction();
throw new Exception('Failed to save model 1');
throw new Exception('Failed to save model 2');
// You can handle success messages and redirection here
catch (Exception $ex)
What Keith said plus see these couple of official documentation on this: first (generic transaction SQL usage) and second (AR with transactions. probably closer to what you want).