Aller au contenu

Missing Transaction For Multi-Write (ARCH7065)

Ce contenu n’est pas encore disponible dans votre langue.

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.

<?php
$account->balance -= $amount;
$account->save();
$target->balance += $amount; // if this fails, money is lost
$target->save();
<?php
DB::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.