Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2406

Merging of new detached entities with PrePersist lifecycle callback breaks

    Details

    • Type: Bug Bug
    • Status: Awaiting Feedback
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.3.2
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:

      Description

      Merging of new detached entities with PrePersist lifecycle callback breaks:

      Code snippet:

          class A
          {
             /**
              *  @ORM\ManyToOne(targetEntity= ...
              *  @ORM\JoinColumn(name=" ...
              */
              protected $b;
              
              public function getB()
              {
                  return $this->b;
              }
              
              public function setB($b)
              {
                  $this->b = $b;
              }
              
              /**
               *
               * @ORM\PrePersist
               *
               * @return void
               */
              public function onPrePersist()
              {
                 if ($this->getB() === null) {
                      throw new \Exception('B instance must be defined);
                 }
                 ....
              }
          }
          
          class B 
          {
          }
          
          $a = new A();
          $b = $em->find('B', 1);
          $a->setB($b);
          $em->persist($a); // works fine as B instance is set
          $em->detach($a);
          
          $a = $em->merge($a) // breaks in onPrePersist
      

      The reason it happens is that the merge operation is trying to persist a new entity created by uow::newInstance($class) without populating its properties first:

       // If there is no ID, it is actually NEW.
          ....
          if ( ! $id) {
              $managedCopy = $this->newInstance($class);
      
              $this->persistNew($class, $managedCopy);
          } else {
      	....
      

      This should happen first for the $managedCopy:

          // Merge state of $entity into existing (managed) entity
          foreach ($class->reflClass->getProperties() as $prop) {
              ....
      

        Activity

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Oleg Namaka
          • Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Created:
              Updated: