DDC-1690: Notify policy listeners are not attached to entities with Notify policy #2126

Closed
opened 2026-01-22 13:41:45 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Mar 8, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user deatheriam:

It seems that it happens because the UnitOfWork::addToIdentityMap() is not the only way to add an entity to the identity map. It is possible to do it directly by assigning array values (pulling my hair why?) . But the code in that method also adds a property changed listener to an entity. As a result that entity is not persisted properly as it does not have a listener. I spent a lot of time trying to figure out why some of my entities were persisted and others no. That happens for entities of same class with parent - children associations.

Probably the notify policy is not very popular otherwise the bug would have been reported long time ago.

Adding these lines to the UnitOfWork at line 2004 fixes the issue:

    // PERF: Inlined & optimized code from UnitOfWork#registerManaged()
    $newValueOid = spl*object*hash($newValue);
    $this->entityIdentifiers[$newValueOid] = $associatedId;
    $this->identityMap[$targetClass->rootEntityName][$relatedIdHash] = $newValue;
    // new lines
 -- if ($newValue instanceof NotifyPropertyChanged) {
 --     $newValue->addPropertyChangedListener($this);
 -- }
    //end of new lines
    $this->entityStates[$newValueOid] = self::STATE_MANAGED;
    // make sure that when an proxy is then finally loaded, $this->originalEntityData is set also!
Originally created by @doctrinebot on GitHub (Mar 8, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user deatheriam: It seems that it happens because the UnitOfWork::addToIdentityMap() is not the only way to add an entity to the identity map. It is possible to do it directly by assigning array values (pulling my hair why?) . But the code in that method also adds a property changed listener to an entity. As a result that entity is not persisted properly as it does not have a listener. I spent a lot of time trying to figure out why some of my entities were persisted and others no. That happens for entities of same class with parent - children associations. Probably the notify policy is not very popular otherwise the bug would have been reported long time ago. Adding these lines to the UnitOfWork at line 2004 fixes the issue: ``` // PERF: Inlined & optimized code from UnitOfWork#registerManaged() $newValueOid = spl*object*hash($newValue); $this->entityIdentifiers[$newValueOid] = $associatedId; $this->identityMap[$targetClass->rootEntityName][$relatedIdHash] = $newValue; // new lines -- if ($newValue instanceof NotifyPropertyChanged) { -- $newValue->addPropertyChangedListener($this); -- } //end of new lines $this->entityStates[$newValueOid] = self::STATE_MANAGED; // make sure that when an proxy is then finally loaded, $this->originalEntityData is set also! ```
admin added the Bug label 2026-01-22 13:41:45 +01:00
admin closed this issue 2026-01-22 13:41:45 +01:00
Author
Owner

@doctrinebot commented on GitHub (Mar 15, 2012):

Comment created by @guilhermeblanco:

Changing the code (Perf & Inline block) to this:

$this->registerManaged($newValue, $associatedId, array($field => $newValue));

Also fixed the issue and the performance impact was around 3% during my tests.

@doctrinebot commented on GitHub (Mar 15, 2012): Comment created by @guilhermeblanco: Changing the code (Perf & Inline block) to this: ``` $this->registerManaged($newValue, $associatedId, array($field => $newValue)); ``` Also fixed the issue and the performance impact was around 3% during my tests.
Author
Owner

@doctrinebot commented on GitHub (Apr 16, 2012):

Comment created by deatheriam:

So Doctrine 2.2.2 is out but the fix for this serious issue was not included in it. What version then it is supposed to fix? Doctrine seems to be riddled with issues related to the Notify strategy (look here for another related issue: http://www.doctrine-project.org/jira/browse/DDC-1775). It renders that strategy unusable.

@doctrinebot commented on GitHub (Apr 16, 2012): Comment created by deatheriam: So Doctrine 2.2.2 is out but the fix for this serious issue was not included in it. What version then it is supposed to fix? Doctrine seems to be riddled with issues related to the Notify strategy (look here for another related issue: http://www.doctrine-project.org/jira/browse/[DDC-1775](http://www.doctrine-project.org/jira/browse/DDC-1775)). It renders that strategy unusable.
Author
Owner

@doctrinebot commented on GitHub (Dec 20, 2012):

Comment created by pschwisow:

Submitted a pull request for this fix: https://github.com/doctrine/doctrine2/pull/541

@doctrinebot commented on GitHub (Dec 20, 2012): Comment created by pschwisow: Submitted a pull request for this fix: https://github.com/doctrine/doctrine2/pull/541
Author
Owner

@doctrinebot commented on GitHub (Dec 22, 2012):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Dec 22, 2012): 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#2126