Is there a way to get doctrine lifecycle events in symfony2 for an association field?
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/events.html#lifecycle-events
eg:
Entity\User.php
..
/**
* @ORM\ManyToMany(targetEntity="Group", inversedBy="users", cascade={"persist"})
* @ORM\JoinTable(name="users_groups",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id", onDelete="CASCADE")}
* )
*/
protected $groups;
So, when I create an event listener for the user
http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html
Listener is called when Entity \ Group is added to Entity \ User.
NOTE. The only way to get this functionality is to create an Entity \ UserGroup and control this postpersist instead of PostPersist users.
Is this the only way to listen to entity associations?
!
, getEntityChangeSet Event Args. getScheduledCollectionUpdates . :
EventListener\PostEventListener
..
public function postUpdate(LifecycleEventArgs $args)
{
$this->handlePostEvents($args);
}
public function postPersist(LifecycleEventArgs $args)
{
$this->handlePostEvents($args);
}
public function handlePostEvents(LifecycleEventArgs $args){
$entity = $args->getEntity();
$em = $args->getEntityManager();
if ($entity instanceof User) {
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledCollectionUpdates() AS $col) {
if ($col->first() instanceof Group) {
}
}
}
}