0

I have this event listener below, but it is not working:

<?php
namespace Project\BackendBundle\EventListener;
//src/Project/BackendBundle/EventListener/ClippedImagesManager.php
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Project\BackendBundle\Entity\Subitem;
class ClippedImagesManager
{
public function preUpdate(LifecycleEventArgs $args)
{
die("Event listener!!!");
}



//src/Project/BackendBundle/Resources/config/services.yml
services:
    project.clipped_images_manager:
        class: Project\BackendBundle\EventListener\ClippedImagesManager
        tags:
            - { name: doctrine.event_listener, event: preUpdate } 

I expected "Event listener!!" was fired when updating any entity inside BackendBundle.

2
  • 1
    Is your services file being loaded? You can use app/console container:debug to verify your service definition is being found. Of course you do have 8k rep so you probably have already checked this.
    – Cerad
    Commented Mar 19, 2015 at 20:56
  • And you do understand that events are only fired on flush()? Not as soon as the entity is changed?
    – Cerad
    Commented Mar 19, 2015 at 23:09

1 Answer 1

1

I had a similar issue before. Stripped off example below is same as yours but to see the full working example visit the post please. The trick is, persisting after preUpdate() within postFlush() event.

Note: Although this might not be the best solution, it could be done with an Event Subscriber or simple onFlush() -> $uow->getScheduledEntityUpdates() in an Event Listener.

Service.yml

services:

    entity.event_listener.user_update:
        class:  Site\FrontBundle\EventListener\Entity\UserUpdateListener
        tags:
            - { name: doctrine.event_listener, event: preUpdate }

Event Listener

<?php

namespace Site\FrontBundle\EventListener\Entity;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Site\FrontBundle\Entity\User;

class UserUpdateListener
{
    public function preUpdate(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        // False check is compulsory otherwise duplication occurs
        if (($entity instanceof User) === false) {
            // Do something
        }
    }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.