This is possible after Doctrine 2.4 with the Entity listenener function.
namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; class Post { } namespace AppBundle\EntityListener; use Doctrine\ORM\Mapping as ORM; class PostListener { public function preUpdate() { }
What if I want to add a dependency to the listener? Is it possible?
Yes, itβs possible starting with DoctrineBundle 1.3. You just need to register the entity listener as a service and tag it with doctrine.orm.entity_listener tag.
class PostListener { public function __construct(SomeDependency $someDependency) { } } services: app.post_listener: class: AppBundle\EntityListener\PostListener arguments: ["@app.some_dependency"] tags: - { name: doctrine.orm.entity_listener }
Alternative method
With DoctrineBundle 1.5, you can register object listeners through tags , but this method has not yet been documented . This method does not require the listener to display with EntityListeners
annotation.
namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; class Post { } namespace AppBundle\EntityListener; class PostListener { public function __construct(SomeDependency $someDependency) { } public function preUpdate() { } public function someOtherName() { } } services: app.post_listener: class: AppBundle\EntityListener\PostListener arguments: ["@app.some_dependency"] tags: - { name: doctrine.orm.entity_listener, entity: AppBundle\Entity\Post, event: preUpdate } # or - { name: doctrine.orm.entity_listener, entity: AppBundle\Entity\Post, event: preUpdate, method: someOtherName }
source share