Nick Price
Nick Price

Reputation: 963

Symfony2/Doctrine - postFlush

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?

Upvotes: 3

Views: 13143

Answers (2)

Pete_Gore
Pete_Gore

Reputation: 634

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.

Upvotes: 5

Michael Sivolobov
Michael Sivolobov

Reputation: 13340

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" }

Upvotes: 1

Related Questions