diff --git a/DependencyInjection/DoctrineMongoDBExtension.php b/DependencyInjection/DoctrineMongoDBExtension.php index d6bd65b7..0ed8c390 100644 --- a/DependencyInjection/DoctrineMongoDBExtension.php +++ b/DependencyInjection/DoctrineMongoDBExtension.php @@ -6,6 +6,7 @@ use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\FixturesCompilerPass; use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass; +use Doctrine\Bundle\MongoDBBundle\EventSubscriber\EventSubscriberInterface; use Doctrine\Bundle\MongoDBBundle\Fixture\ODMFixtureInterface; use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepositoryInterface; use Doctrine\Common\DataFixtures\Loader as DataFixturesLoader; @@ -101,6 +102,9 @@ public function load(array $configs, ContainerBuilder $container) $container->registerForAutoconfiguration(ServiceDocumentRepositoryInterface::class) ->addTag(ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG); + + $container->registerForAutoconfiguration(EventSubscriberInterface::class) + ->addTag('doctrine_mongodb.odm.event_subscriber'); } /** diff --git a/EventSubscriber/EventSubscriberInterface.php b/EventSubscriber/EventSubscriberInterface.php new file mode 100644 index 00000000..7002ae96 --- /dev/null +++ b/EventSubscriber/EventSubscriberInterface.php @@ -0,0 +1,11 @@ + + + + + + + + + +Alternatively, use the ``doctrine_mongodb.odm.event_subscriber`` tag to register a subscriber. Subscribers must implement the ``Doctrine\Common\EventSubscriber`` interface, which means that they must contain method returning the events they will observe. For this reason, this tag has no ``event`` attribute, however the ``connection``, ``priority`` and ``lazy`` attributes are available. +.. code-block:: php + + // src/App/EventSubscriber/MongoDB/ProductSubscriber.php + namespace App\EventSubscriber\MongoDB; + + use App\Document\Product; + use Doctrine\Common\EventSubscriber; + + class ProductSubscriber implements EventSubscriber + { + public function getSubscribedEvents() + { + return [ + // List events to subscribe + ]; + } + } + +.. configuration-block:: + + .. code-block:: yaml + + # config/services.yaml + services: + + App\EventSubscriber\MongoDB\: + resource: '../src/EventSubscriber/MongoDB/*' + tags: + - { name: doctrine_mongodb.odm.event_subscriber } + + .. code-block:: xml + + + + + + + + + + + + .. note:: Unlike Symfony event listeners, Doctrine's event manager expects each @@ -80,3 +173,4 @@ this tag has no ``event`` attribute, however the ``connection``, .. _`event dispatcher`: https://symfony.com/doc/current/components/event_dispatcher.html .. _`Event Documentation`: https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/events.html .. _`tagging`: https://symfony.com/doc/current/service_container/tags.html +.. _`autoconfiguration`: https://symfony.com/doc/current/service_container.html#the-autoconfigure-option