Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Security Level: All
    • 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.

        Activity

        Hide
        Marco Pivetta added a comment -

        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);
            }
        }
        
        Show
        Marco Pivetta added a comment - 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); } }
        Hide
        Marco Pivetta added a comment -

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

        Show
        Marco Pivetta added a comment - Not an issue - unsafe usage of the `preUpdate` event
        Hide
        Petter Castro added a comment -

        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,

        Show
        Petter Castro added a comment - 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,
        Hide
        Marco Pivetta added a comment -

        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.

        Show
        Marco Pivetta added a comment - 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.
        Hide
        Petter Castro added a comment -

        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.

        Show
        Petter Castro added a comment - 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.

          People

          • Assignee:
            Marco Pivetta
            Reporter:
            Petter Castro
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: