Skip to content

[Messenger] Show how to configure a doctrine transaction middleware #10013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions messenger/working-with-doctrine.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
.. index::
single: Messenger; Working with Doctrine

Working with Doctrine
=====================

If your message handlers writes to a database it is a good idea to wrap all those
writes in a single Doctrine transaction. This make sure that if one of your database
query fails, then all queries are rolled back and give you a chance to handle the
exception knowing that your database was not changed by your message handler(s).

Next thing you need to do is to add the middleware to your bus configuration.

.. configuration-block::

.. code-block:: yaml

# config/packages/messenger.yaml
framework:
# ...
buses:
messenger.bus.command:
middleware:
- validation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we purposely also including validation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a bit unrelated with the content of this doc indeed, but a good practice, as content of the command in a CQRS application are most of the input by users (on contrary of events which are mostly crafted by developers from already validated data).

- doctrine_transaction

.. code-block:: xml

<!-- config/packages/messenger.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:messenger>
<framework:bus name="messenger.bus.commands">
<framework:middleware id="validation"/>
<framework:middleware id="doctrine_transaction"/>
<framework:bus>
</framework:messenger>
</framework:config>
</container>

.. code-block:: php

// config/packages/messenger.php
$container->loadFromExtension('framework', [
'messenger' => [
'buses' => [
'messenger.bus.commands' => [
'middleware' => [
'validation',
'doctrine_transaction',
],
],
],
],
]);