Skip to content

Commit 782636e

Browse files
committed
docs: clarify locked row rollback semantics
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 4096877 commit 782636e

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

‎user_guide_src/source/models/model.rst‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -791,13 +791,15 @@ the transaction status fails without an exception, ``false`` is returned.
791791
Existing Model query constraints are applied to the locked lookup, so methods
792792
like ``where()`` and ``withDeleted()`` can be called before ``withLockedRow()``.
793793

794-
Any exception thrown by the callback rolls back the transaction and is rethrown.
795-
Unsupported lock combinations throw the same database exceptions as
796-
``lockForUpdate()``.
797-
798-
The locked row is reloaded directly from the database, so Model find callbacks
799-
are not run for that lookup. Model methods called inside the callback, such as
800-
``save()`` or ``update()``, still follow the Model's callback setting.
794+
Returning ``false`` from the callback does not trigger a rollback. To explicitly
795+
roll back from the callback, throw an exception. Any exception thrown by the
796+
callback rolls back the transaction and is rethrown. Unsupported lock
797+
combinations throw the same database exceptions as ``lockForUpdate()``.
798+
799+
.. important:: The locked row is reloaded directly from the database, so Model
800+
find callbacks are not run for that lookup. Model methods called inside the
801+
callback, such as ``save()`` or ``update()``, still follow the Model's
802+
callback setting.
801803

802804
.. note:: Row locks are only useful when the database supports transactions and
803805
pessimistic locks. See :ref:`query-builder-lock-for-update` for driver

‎user_guide_src/source/models/model/073.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
$result = $accountModel->withLockedRow($id, static function (object $account, $model): bool {
44
$account->balance -= 100;
55

6-
return $model->save($account);
6+
if (! $model->save($account)) {
7+
throw new RuntimeException('Unable to save the account.');
8+
}
9+
10+
return true;
711
});

0 commit comments

Comments
 (0)