DDC-2704: When using Discriminator EntityManager#merge fails #3386

Closed
opened 2026-01-22 14:19:02 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Sep 25, 2013).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user vladykx:

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) {
Originally created by @doctrinebot on GitHub (Sep 25, 2013). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user vladykx: 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) { ```
admin added the Bug label 2026-01-22 14:19:02 +01:00
admin closed this issue 2026-01-22 14:19:02 +01:00
Author
Owner
@doctrinebot commented on GitHub (Sep 25, 2013): - depends on [DDC-3524: [GH-1272] [DDC-2704] - merge inherited transient properties - merge properties into uninitialized proxies](http://www.doctrine-project.org/jira/browse/DDC-3524)
Author
Owner

@doctrinebot commented on GitHub (Jan 20, 2015):

Comment created by rkj:

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

@doctrinebot commented on GitHub (Jan 20, 2015): Comment created by rkj: 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
Author
Owner

@doctrinebot commented on GitHub (Jan 20, 2015):

Comment created by @ocramius:

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.

@doctrinebot commented on GitHub (Jan 20, 2015): Comment created by @ocramius: 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.
Author
Owner

@doctrinebot commented on GitHub (Jan 25, 2015):

Comment created by @ocramius:

Handled in DDC-3524

@doctrinebot commented on GitHub (Jan 25, 2015): Comment created by @ocramius: Handled in [DDC-3524](http://www.doctrine-project.org/jira/browse/DDC-3524)
Author
Owner

@doctrinebot commented on GitHub (Jan 25, 2015):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Jan 25, 2015): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3386