DoctrineDBALBridge¶
This package provides a command bus middleware that can be used to integrate SimpleBus/MessageBus with Doctrine DBAL.
It provides an easy way to wrap command handling in a database transaction.
@TODO The intro should explain what it does.
Getting started¶
Preparations¶
To use the middleware provided by the library, set up a command bus, if you didn’t already do this:
1 2 3 4 | use SimpleBus\Message\Bus\Middleware\MessageBusSupportingMiddleware;
$commandBus = new MessageBusSupportingMiddleware();
...
|
Make sure to also properly set up a Doctrine connection:
1 2 | // $connection is an instance of Doctrine\DBAL\Driver\Connection
$connection = ...;
|
Transactions¶
It is generally a good idea to wrap command handling in a database
transaction. If you want to do this, add the
WrapsMessageHandlingInTransaction
middleware to the command bus.
Provide an instance of the Doctrine Connection
interface that you
want to use.
1 2 3 4 5 6 7 8 | use SimpleBus\DoctrineDBALBridge\MessageBus\WrapsMessageHandlingInTransaction;
// $connection is an instance of Doctrine\DBAL\Driver\Connection
$connection = ...;
$transactionalMiddleware = new WrapsMessageHandlingInTransaction($connection);
$commandBus->addMiddleware($transactionalMiddleware);
|
When an exception is thrown, the transaction will be rolled back. If not, the transaction is committed.