I have two models, Profesional and Tramite. The first one represents the data of an agricultural engineer who has to register in a public record, and the second one represents the inscription and future renewals.
So, the two models have to be saved at the same time. No problem with that, I use the same form to collect data from the two models, and save them like this
if ($modelp->validate() && $modelt->validate()) {
if ($modelp->save()) {
$modelp->link('tramites', $modelt);
return $this->redirect(['view', 'id' => $modelp->id]);
}
}
It works, but I was wondering. Is there a form to undo the insert of the first model, if for some circunstance, the insert of the second one fail? Is this doable with AR or I should use queries in order to perform a transaction?
Yes, but I wasn’t asking how to save related models, I already have that cover, as you can see in my original question. The question was about the posibility of using transactions with AR, to do a rollback if the insert of the child fails, so it won’t be a father without it’s correspondent child.
Sorry, I didn’t look at the second one. I wonder if there is an equivalent of EAdvancedArBehavior for Yii2. I will have to do a little research. Thanks!