[DDC-2512] Lifecycle callbacks Created: 17/Jun/13  Updated: 03/Jul/13  Resolved: 17/Jun/13

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: None
Affects Version/s: None
Fix Version/s: None
Security Level: All

Type: Bug Priority: Major
Reporter: Petter Castro Assignee: Marco Pivetta
Resolution: Invalid Votes: 0
Labels: None
Environment:

windows xp



 Description   

Hi,

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();
$unitOfWork->scheduleForInsert($log);

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

Thanks for your help.



 Comments   
Comment by Marco Pivetta [ 17/Jun/13 ]

It is not safe to schedule operations while they are happening. Please use the onFlush event instead, as described in http://stackoverflow.com/questions/15092965/create-entity-on-entitiy-flush

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));
            }
        }

        $uow->computeChangeSets();
    }

    public function getSubscribedEvents()
    {
        return array(Events::onFlush);
    }
}
Comment by Marco Pivetta [ 17/Jun/13 ]

Not an issue - unsafe usage of the `preUpdate` event

Comment by Petter Castro [ 24/Jun/13 ]

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

$uow->getEntityChangeSet($entity);

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 by Marco Pivetta [ 24/Jun/13 ]

Petter Castro 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 by Petter Castro [ 03/Jul/13 ]

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.

Thanks.

Generated at Wed Jul 30 21:18:42 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.