Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2704

When using Discriminator EntityManager#merge fails

    Details

      Description

      I have the following hierarchy:

      • AgentConfig has relation ManyToOne with AgentTask.
      • AgentTask has DiscriminatorColumn & DiscriminatorMap assigned to it.
      • AgentTask has relation ManyToOne with AgentTaskConfig.

      I believe the problem is because of the following:

      UnitOfWork#doMerge has the tries to get properties the following way:

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

      This obviously doesn't get the parent class (AgentTask) properties.

      Later on UnitOfWork fails on line:

      $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy);
      

      because $prevManagedCopy doesn't have properties set from entity.

      My proposal is to get the properties the following way:

      $properties = $class->reflClass->getProperties();
      $parent = $class->reflClass;
      while (($parent = $parent->getParentClass()) != null) {
          $properties = array_merge($parent->getProperties(), $properties);
      }
      
      // Merge state of $entity into existing (managed) entity
      foreach ($properties as $prop) {
      

        Issue Links

          Activity

          Hide
          Rasmus Jensen added a comment -

          I have this problem as well in .
          The Merge operation does not copy properties of any parent class to the managed copy. My workaround when merging entities with inheritance is:
          1. Receive detached entity that is instance of a subclass.
          2. fetch a managed copy of the entity from database with id of detached entity.
          3. manually set properties on the managed entity.
          4. flush to save changes.

          This is obviously only manageable with a relatively simple datastructure, and quickly becomes very messy with more complex entities and relationship hierarchies.

          Would really appreciate a fix for this

          Show
          Rasmus Jensen added a comment - I have this problem as well in . The Merge operation does not copy properties of any parent class to the managed copy. My workaround when merging entities with inheritance is: 1. Receive detached entity that is instance of a subclass. 2. fetch a managed copy of the entity from database with id of detached entity. 3. manually set properties on the managed entity. 4. flush to save changes. This is obviously only manageable with a relatively simple datastructure, and quickly becomes very messy with more complex entities and relationship hierarchies. Would really appreciate a fix for this
          Hide
          Marco Pivetta added a comment -

          A temporary workaround is to change properties into protected. I'll mark this for 2.5 and see if it can be fixed by then.

          Show
          Marco Pivetta added a comment - A temporary workaround is to change properties into protected. I'll mark this for 2.5 and see if it can be fixed by then.
          Hide
          Marco Pivetta added a comment -

          Handled in DDC-3524

          Show
          Marco Pivetta added a comment - Handled in DDC-3524

            People

            • Assignee:
              Marco Pivetta
              Reporter:
              Vladyslav Petrovych
            • Votes:
              2 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: