Skip to content

Commit

Permalink
Add documentation about transactions to event docs
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jan 16, 2024
1 parent e064235 commit a1acb9d
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/en/reference/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,38 @@ follow this restrictions very carefully since operations in the
wrong event may produce lots of different errors, such as
inconsistent data and lost updates/persists/removes.

Handling Transactional Flushes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When a flush operation is executed in a transaction, all queries inside a lifecycle event listener also have to make use
of the session used during the flush operation. This session object is exposed through the ``LifecycleEventArgs``
parameter passed to the listener. Passing the session to queries ensures that the query will become part of the
transaction and will see data that has not been committed yet.

.. code-block:: php
<?php
public function someEventListener(\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs $eventArgs): void
{
// To check if a transaction is active:
if ($eventArgs->isInTransaction()) {
// Do something
}
// Pass the session to any query you execute
$eventArgs->getDocumentManager()->createQueryBuilder(User::class)
// Query logic
->getQuery(['session' => $eventArgs->session])
->execute();
}
.. note::

Event listeners are only called during the first transaction attempt. If the transaction is retried, event listeners
will not be invoked again. Make sure to run any persistence logic through the UnitOfWork instead of modifying data
directly through queries run in an event listener.

prePersist
~~~~~~~~~~

Expand Down

0 comments on commit a1acb9d

Please sign in to comment.