2

Does Doctrine have an event that triggers when you add or remove an entry from a doctrine collection that is part of a ManyToMany relationship?

I basically have an Entity which only has an id and a Collection of Tags in a ManyToMany relationship. However, I want to be able to run an event listener when a Tag is added or removed from the Entity. Is there any way to do this?

Erik
  • 1,057
  • 7
  • 11
  • You could create a custom event. http://stackoverflow.com/questions/11661057/how-to-create-custom-event-in-symfony2 – Markus Kottländer Sep 23 '14 at 15:05
  • But how would the custom event know that the ArrayCollection changed? I'd rather not put this event-dispatch logic in every controller that modifies the Entity as there'll be a lot and I don't want to miss any. – Erik Sep 23 '14 at 15:13
  • 1
    Perhaps this approach? http://egeloen.fr/2013/12/01/symfony2-doctrine2-entity-listener-as-serice/ – Peter Bailey Sep 23 '14 at 16:02
  • I agree the approach of Markus & Peter. You should create a listener service for the load and persist events, store the collection state at load, make a diff at persist, and trigger a custom event if the collection changed... This is pretty dirty, but I don't think you will find a native mecanics that suit to your needs... – Yann Eugoné Sep 24 '14 at 09:25

0 Answers0