简体   繁体   中英

Symfony2 Doctrine Listener postPersist not invoking

I've been following example code to try and get my doctrine event listener to work. However, even though the class is being instantiated into an object (I know this because I logged the __construct, and the __destructor also gets invoked, the postPersist function never does.

My services.yml file has the following (located in AH/Core/SolutionBundle/Resources/config/services.yml):

solutions_core_reverse_sync:
    class: AH\Core\SolutionBundle\Listener\ClientSolutionReverseSyncListener
    arguments: [@service_container]
    tags:
        - { name: doctrine.event_listener, event: postPersist }

(Also, the services.yml file is being loaded in AH/Core/SolutionBundle/DependencyInjection/SolutionExtension.php - confirmed because other services are running just fine)

My Entity is just a standard doctrine entity, nothing special about it, except for using a few extra annotation based integrations, like the JMS Serializer. The only thing that is different to most other entities, is the fact that we use the standard SingleTableInheritence from Doctrine, using an @ORM\\DiscriminatorMap annotation and the child-entities.

My listener only has a skeleton right now, to test whether it works without anything interfering:

<?php
namespace AH\Core\SolutionBundle\Listener;

use Symfony\Component\DependencyInjection\Container;
use Doctrine\ORM\Event\LifecycleEventArgs;

class ClientSolutionReverseSyncListener
{
    protected $container;

    public function __construct(Container $container)
    {
        $this->container = $container;

        echo __CLASS__.' __construct'.PHP_EOL;
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        echo __CLASS__.' postPersist fired'.PHP_EOL;
    }

    public function __destruct()
    {
        echo __CLASS__.' __destruct'.PHP_EOL;
    }
}

When testing it, and running the below code, I only see the __construct and the __destruct run (by way of echoing) but not the postPersist:

$cs = $csm->findClientSolutionById(123); // don't worry where $csm comes from
$cs->setUid('do some update: '.rand(0,10000));
$this->em->persist($cs);

Sample output:

AH\\Core\\SolutionBundle\\Listener\\ClientSolutionReverseSyncListener __construct AH\\Core\\SolutionBundle\\Listener\\ClientSolutionReverseSyncListener __destruct

I'm at a loss of where I went wrong here, it follows the docs super close: Doctrine documentation

I also checked this doc, which is similar to the above: Symfony documentation around listeners

Here is the explanation , and the implementation So, you need to flush the changes, if you want the event to be fired. Persisting entities without flushing them doesn't generate primary key. Also persisting entities doesn't call the database insert operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM