Symfony2/Doctrine - postFlush

2019-05-18 09:45发布

If you saw my previous question, this is kind of linked to it but a new question. So I have an Entity and I have a listener linked up to this. In my createAction I create my Object and then persist-flush it to my database. In my listener, I have set up a postFlush function

public function postFlush(PostFlushEventArgs $args)
{
    $em = $args->getEntityManager();

    foreach ($em->getUnitOfWork()->getScheduledEntityDeletions() as $entity) {
        if ($entity instanceof AvailabilityAlert) {
            var_dump("TEST");
            $this->api_service->addFlightsAction($entity);
        }
    }
}

What I am trying to do in this function is get the entity that was just flushed. I have tried all the different actions of getUnitsOfWork e.g. getScheduledEntityDeletions but for none of them I can get into where the var_dump occurs.

How would I go about getting the flushed entity within this postFlush function?

2条回答
家丑人穷心不美
2楼-- · 2019-05-18 09:54

To get just persisted to database entity you need not postFlush but postPersist:

public function postPersist(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();
}

And don't forget to add next tag to your service:

{ name: "doctrine.event_listener",  event: "postPersist" }
查看更多
Root(大扎)
3楼-- · 2019-05-18 10:02

According to Doctrine2 documentation here : http://doctrine-orm.readthedocs.org/en/latest/reference/events.html you can't call the flushed entities on PostFlush event.

However you can split your logic : use the OnFlush event to get these entities and then pass it to the PostFlush if the flush succeeded.

查看更多
登录 后发表回答