Is there a way to fire afterCreate and afterSave events only after the transaction is commited succesfully?
// Controller
$t = new TransactionManager()->get()->throwRollbackException(true);
try {
...
$a->setTransaction($t);
$b->setTransaction($t);
if (!$a->save()) {
$t->rollback();
}
// here the events for afterCreate / afterSave for model a are
// fired even thought the transaction was not commited
if (!$b->save()) {
$t->rollback();
}
$t->rollback('stop the transaction to test model events');
$t->commit();
}
catch (Exception $e) {
var_dump('if rollback, code should each here without running any afterSave event');
}
// Model
public function afterSave() {
var_dump('should run after the data exists in the database');
}