DDC-3619: spl_object_hash collision #4445

Closed
opened 2026-01-22 14:41:46 +01:00 by admin · 10 comments
Owner

Originally created by @doctrinebot on GitHub (Mar 17, 2015).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user Akhristenko:

The code below demonstrate problem.

$user = $entityManager->find(User::class, $id); // get entity from db
$entityManager->remove($user); // remove user
$entityManager->persist($user); // cancel remove
unset($user);
/** at this moment spl_object_hash($user) is presented in the UoW, but the $user isn't presented in the identity map in UoW **/
$user2 = new User(); // spl*object_hash($user2) may be equal to spl_object*hash($user) because $user has no reference
$entityManager->persist($user2);
$entityManager->flush(); //$user2 will not be saved, because UoW think, that is already exists
Originally created by @doctrinebot on GitHub (Mar 17, 2015). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user Akhristenko: The code below demonstrate problem. ``` $user = $entityManager->find(User::class, $id); // get entity from db $entityManager->remove($user); // remove user $entityManager->persist($user); // cancel remove unset($user); /** at this moment spl_object_hash($user) is presented in the UoW, but the $user isn't presented in the identity map in UoW **/ $user2 = new User(); // spl*object_hash($user2) may be equal to spl_object*hash($user) because $user has no reference $entityManager->persist($user2); $entityManager->flush(); //$user2 will not be saved, because UoW think, that is already exists ```
admin added the Bug label 2026-01-22 14:41:46 +01:00
admin closed this issue 2026-01-22 14:41:46 +01:00
Author
Owner
@doctrinebot commented on GitHub (Mar 17, 2015): - depends on [DDC-3624: [GH-1338] [DDC-3619] Update identityMap when entity gets managed again](http://www.doctrine-project.org/jira/browse/DDC-3624) - is referenced by [DDC-3624: [GH-1338] [DDC-3619] Update identityMap when entity gets managed again](http://www.doctrine-project.org/jira/browse/DDC-3624)
Author
Owner

@doctrinebot commented on GitHub (Mar 17, 2015):

Comment created by @ocramius:

{quote}spl_object_hash($user2) may be equal to spl_object_hash($user) because $user has no reference{quote}

this seems wrong, since $user won't get garbage collected

@doctrinebot commented on GitHub (Mar 17, 2015): Comment created by @ocramius: {quote}spl_object_hash($user2) may be equal to spl_object_hash($user) because $user has no reference{quote} this seems wrong, since `$user` won't get garbage collected
Author
Owner

@doctrinebot commented on GitHub (Mar 17, 2015):

Comment created by Akhristenko:

Why? The $user has no reference to it, so it may be collected.
I use SoftDeleteabale extension and faced with this problem.
My code is look like:

function delete($id) {
  $entity = $entityManager->find(Entity::class, $id);
  $entityManager->delete($entity); // the softdeleteable extension call persist($entity) and remove $entity from sheduledForDeletion in onFlush
  $entityManager->flush($entity);
} // after this function the UoW contains spl*object*hash($entity), but not contains $entity. After leave function $entity collected.
function createAnother() {
  $anotherEntity = new AnotherEntity();
  $entityManager->persist($anotherEntity);
  $entityManager->flush($anotherEntity); // there is a spl*object*hash collision and the entity is not saved
}
function main() {
  //...
  delete($id);
  //...
  createAnother();
}
@doctrinebot commented on GitHub (Mar 17, 2015): Comment created by Akhristenko: Why? The $user has no reference to it, so it may be collected. I use SoftDeleteabale extension and faced with this problem. My code is look like: ``` function delete($id) { $entity = $entityManager->find(Entity::class, $id); $entityManager->delete($entity); // the softdeleteable extension call persist($entity) and remove $entity from sheduledForDeletion in onFlush $entityManager->flush($entity); } // after this function the UoW contains spl*object*hash($entity), but not contains $entity. After leave function $entity collected. function createAnother() { $anotherEntity = new AnotherEntity(); $entityManager->persist($anotherEntity); $entityManager->flush($anotherEntity); // there is a spl*object*hash collision and the entity is not saved } function main() { //... delete($id); //... createAnother(); } ```
Author
Owner

@doctrinebot commented on GitHub (Mar 17, 2015):

Comment created by @ocramius:

[~Akhristenko] the UoW has an internal reference to $user, so it cannot be garbage collected

@doctrinebot commented on GitHub (Mar 17, 2015): Comment created by @ocramius: [~Akhristenko] the UoW has an internal reference to `$user`, so it cannot be garbage collected
Author
Owner

@doctrinebot commented on GitHub (Mar 17, 2015):

Comment created by @ocramius:

I suggest abstracting this problem into a test case in order to remove these doubts upfront.

@doctrinebot commented on GitHub (Mar 17, 2015): Comment created by @ocramius: I suggest abstracting this problem into a test case in order to remove these doubts upfront.
Author
Owner

@doctrinebot commented on GitHub (Mar 17, 2015):

Comment created by Akhristenko:

{quote}
Aleksandr Khristenko the UoW has an internal reference to $user, so it cannot be garbage collected
{quote}
Yes, but when we call remove($entity) this internal reference move from identityMap to entityDeletions array. And when we after that call persist($entity) this internal reference remove from entityDeletions array but NOT added to identityMap. So, after that UoW has not an internal reference to $user.

@doctrinebot commented on GitHub (Mar 17, 2015): Comment created by Akhristenko: {quote} Aleksandr Khristenko the UoW has an internal reference to $user, so it cannot be garbage collected {quote} Yes, but when we call remove($entity) this internal reference move from identityMap to entityDeletions array. And when we after that call persist($entity) this internal reference remove from entityDeletions array but NOT added to identityMap. So, after that UoW has not an internal reference to $user.
Author
Owner

@doctrinebot commented on GitHub (Mar 17, 2015):

Comment created by Akhristenko:

I attached the test, which demonstrates that reference to entity from UoW is lost.

@doctrinebot commented on GitHub (Mar 17, 2015): Comment created by Akhristenko: I attached the test, which demonstrates that reference to entity from UoW is lost.
Author
Owner

@doctrinebot commented on GitHub (Mar 17, 2015):

Comment created by nclavaud:

I am also using SoftDeleteable Doctrine extension (https://github.com/Atlantic18/DoctrineExtensions) and facing a similar issue.

My scenario is:

  • fetch 2 entities from db ;
  • soft-delete these 2 entities (then flush) ;
  • create 3 new entities and persist them (then flush).

Problem is: 1 of the 3 entities won't be persisted (same $oid than one of the soft-deleted ones).

I've created a PR here that solves my issue:
https://github.com/doctrine/doctrine2/pull/1338

Does it make sense to you?

@doctrinebot commented on GitHub (Mar 17, 2015): Comment created by nclavaud: I am also using SoftDeleteable Doctrine extension (https://github.com/Atlantic18/DoctrineExtensions) and facing a similar issue. My scenario is: - fetch 2 entities from db ; - soft-delete these 2 entities (then flush) ; - create 3 new entities and persist them (then flush). Problem is: 1 of the 3 entities won't be persisted (same $oid than one of the soft-deleted ones). I've created a PR here that solves my issue: https://github.com/doctrine/doctrine2/pull/1338 Does it make sense to you?
Author
Owner

@doctrinebot commented on GitHub (Mar 17, 2015):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1338] was closed:
https://github.com/doctrine/doctrine2/pull/1338

@doctrinebot commented on GitHub (Mar 17, 2015): Comment created by @doctrinebot: A related Github Pull-Request [GH-1338] was closed: https://github.com/doctrine/doctrine2/pull/1338
Author
Owner

@doctrinebot commented on GitHub (Mar 17, 2015):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Mar 17, 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#4445