4
4
Record Events Produced by a Handler
5
5
===================================
6
6
7
- In a example application there is a command (a CQRS message) named ``CreateUser ``.
8
- That command is handled by the ``CreateUserHandler ``. The command handler creates
7
+ In an example application there is a command (a CQRS message) named ``CreateUser ``.
8
+ That command is handled by the ``CreateUserHandler `` which creates
9
9
a ``User `` object, stores that object to a database and dispatches an ``UserCreatedEvent ``.
10
10
That event is also a normal message but is handled by an *event * bus.
11
11
12
12
There are many subscribers to the ``UserCreatedEvent ``, one subscriber may send
13
13
a welcome email to the new user. Since we are using the ``DoctrineTransactionMiddleware ``
14
14
we wrap all database queries in one database transaction and rollback that transaction
15
- if an exception is thrown. That would mean that if an exception is thrown when sending
15
+ if an exception is thrown. That means that if an exception is thrown when sending
16
16
the welcome email, then the user will not be created.
17
17
18
18
The solution to this issue is to not dispatch the ``UserCreatedEvent `` in the
@@ -27,7 +27,7 @@ in the middleware chain.
27
27
28
28
.. code-block :: yaml
29
29
30
- # config/packages/workflow .yaml
30
+ # config/packages/messenger .yaml
31
31
framework :
32
32
messenger :
33
33
default_bus : messenger.bus.command
@@ -45,6 +45,11 @@ in the middleware chain.
45
45
46
46
.. code-block :: php
47
47
48
+ namespace App\Messenger\CommandHandler;
49
+
50
+ use App\Entity\User;
51
+ use App\Messenger\Command\CreateUser;
52
+ use App\Messenger\Event\UserCreatedEvent;
48
53
use Doctrine\ORM\EntityManagerInterface;
49
54
use Symfony\Component\Messenger\MessageRecorderInterface;
50
55
0 commit comments