44Record Events Produced by a Handler
55===================================
66
7- Ina example application there is a command (a CQRS message) named ``CreateUser ``.
8- That command is handled by the ``CreateUserHandler ``. The command handler creates
7+ Inan example application there is a command (a CQRS message) named ``CreateUser ``.
8+ That command is handled by the ``CreateUserHandler `` which creates
99a ``User `` object, stores that object to a database and dispatches an ``UserCreatedEvent ``.
1010That event is also a normal message but is handled by an *event * bus.
1111
1212There are many subscribers to the ``UserCreatedEvent ``, one subscriber may send
1313a welcome email to the new user. Since we are using the ``DoctrineTransactionMiddleware ``
1414we wrap all database queries in one database transaction and rollback that transaction
15- if an exception is thrown. Thatwould mean that if an exception is thrown when sending
15+ if an exception is thrown. Thatmeans that if an exception is thrown when sending
1616the welcome email, then the user will not be created.
1717
1818The solution to this issue is to not dispatch the ``UserCreatedEvent `` in the
@@ -27,7 +27,7 @@ in the middleware chain.
2727
2828 ..code-block ::yaml
2929
30- # config/packages/workflow .yaml
30+ # config/packages/messenger .yaml
3131framework :
3232messenger :
3333default_bus :messenger.bus.command
@@ -45,6 +45,11 @@ in the middleware chain.
4545
4646 ..code-block ::php
4747
48+ namespace App\Messenger\CommandHandler;
49+
50+ use App\Entity\User;
51+ use App\Messenger\Command\CreateUser;
52+ use App\Messenger\Event\UserCreatedEvent;
4853 use Doctrine\ORM\EntityManagerInterface;
4954 use Symfony\Component\Messenger\MessageRecorderInterface;
5055