Skip to content

DDC-2512: Lifecycle callbacks #3235

doctrinebot opened this Issue Jun 17, 2013 · 6 comments

2 participants


Jira issue originally created by user pcastrotigre:


I would like to know if the only one thing i need to save an entity in a lifecycle listener is the $unitOfWork->scheduleForInsert($entity).

My requirement is to save a log in every action in the DB. So i need to save the old entity and the new entity in the DB. I am making this in the preupdate listener:

$log = $this->util->createLog($entity->serialize(),"Current Object");             
$unitOfWork    = $this->wrapped->getUnitOfWork();                

But this log is never saved. Do i need something else to persist this new entity?.

Thanks for your help.


Comment created by @ocramius:

It is not safe to schedule operations while they are happening. Please use the onFlush event instead, as described in

Copying the example for reference:

namespace YourApp\Subscriber;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use YourApp\Entity\Issue;
use YourApp\Entity\IssueLog;

class IssueUpdateSubscriber implements EventSubscriber
    public function onFlush(OnFlushEventArgs $args)
        $em  = $args->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityUpdates() as $updated) {
            if ($updated instanceof Issue) {
                $em->persist(new IssueLog($updated));


    public function getSubscribedEvents()
        return array(Events::onFlush);

Comment created by @ocramius:

Not an issue - unsafe usage of the preUpdate event


Issue was closed with resolution "Invalid"


Comment created by pcastrotigre:

In this method (onflush) I get the new entity (updated).

How could I get the old entity with old values?... I would not like to use the


because this one only returns every field modified, but I would like to save the whole old entity in my DB.

Even in the PreUpdate method, the entity gotten, is the new one, but i also need the old one.

Thanks for your help,


Comment created by @ocramius:

[~pcastrotigre] there is no "new entity" or "old entity": there's the old and the new values for it. The object being held in memory is always the same.


Comment created by pcastrotigre:

This way i could get the old values for primitive properties, but what about relationships?.. How could i get the old relationship and the new relationship ( This is a many to many relationship )

I am doing this:

$group = $eventArgs->getOldValue("groups");

Where "groups" is my relationship. But this throws an error saying "groups" is not a valid field for the entity.


@Ocramius Ocramius was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.