Missing Transaction For Multi-Write (ARCH7065)
Key php.orm.missing-transaction-for-multi-write · Severity warning · Category orm
Not wrapping two or more dependent persistence operations in the same method in an explicit transaction/atomic block risks partial commits. If the first write succeeds and the second fails, data is left inconsistent.
Incorrect
Section titled “Incorrect”<?php$account->balance -= $amount;$account->save();
$target->balance += $amount; // if this fails, money is lost$target->save();Correct
Section titled “Correct”<?phpDB::transaction(function () use ($account, $target, $amount) { $account->balance -= $amount; $account->save();
$target->balance += $amount; $target->save();});Wrap dependent writes in a single transaction/atomic block.