diff --git a/messenger/working-with-doctrine.rst b/messenger/working-with-doctrine.rst new file mode 100644 index 00000000000..317477b0729 --- /dev/null +++ b/messenger/working-with-doctrine.rst @@ -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 + - doctrine_transaction + + .. code-block:: xml + + + + + + + + + + + + + + + + .. code-block:: php + + // config/packages/messenger.php + $container->loadFromExtension('framework', [ + 'messenger' => [ + 'buses' => [ + 'messenger.bus.commands' => [ + 'middleware' => [ + 'validation', + 'doctrine_transaction', + ], + ], + ], + ], + ]); +