-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 change to handle the | ||
exception knowing that your database was not changed by your message handler. | ||
Nyholm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we purposely also including validation? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
], | ||
], | ||
], | ||
], | ||
]); | ||
|
Uh oh!
There was an error while loading. Please reload this page.