What’s your opinion about this snippet code? (I’ve wrote some questions in comments of code)
function training($model_1, $model_2)
{
$transaction = Discount::getDb()->beginTransaction();
try {
$result = true;
if (!$model_1->save()) {
$result = false; // Can i use here "return false;" instead?
}
if ($result) {
if(!$model_2->save()) {
$result = false; // or there can i use here "return false;" instead?
}
}
if ($result) {
$transaction->commit();
return $result;
}
} catch (\Throwable $e) {
$transaction->rollBack();
throw $e;
}
$transaction->rollBack(); // rollback in here is need?
return $result;
}
If you don’t call the commit function your changes won’t be applied to your DB. So when you exit the core previously (by returning something for example) the changes are not applied but it’s better to throw an exception and do the rollback.
I know it, but I want nothing query is left inside the transaction for the next possible transactions, before end of process the current script.
Indeed is need to empty the queue of transportation? If yes, how do I do that?