8000 Spl_object_hash conflict on Merge by moroine · Pull Request #1465 · doctrine/orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Spl_object_hash conflict on Merge #1465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -1896,9 +1896,39 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, $ass

$this->cascadeMerge($entity, $managedCopy, $visited);

// Restore previous state (where the given entity was previously managed)
if ($managedCopy !== $entity) {
$this->forgetEntity($entity);
}

return $managedCopy;
}

/**
* Remove UOW reference to this entity.
*
* WARNING: could cause some issue, created to fix MergeSplHashConflict
*
* @param object $entity
*/
private function forgetEntity($entity)
{
$oid = spl_object_hash($entity);

if ($this->isInIdentityMap($entity)) {
$this->removeFromIdentityMap($entity);
}

unset(
$this->entityInsertions[$oid],
$this->entityUpdates[$oid],
$this->entityDeletions[$oid],
$this->entityIdentifiers[$oid],
$this->entityStates[$oid],
$this->originalEntityData[$oid]
);
}

/**
* Tests if an entity is loaded - must either be a loaded proxy or not a proxy
*
Expand Down
78 changes: 78 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/MergeSplHashConflictTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Query;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\OrmFunctionalTestCase;

class MergeSplHashConflictTest extends OrmFunctionalTestCase
{
protected function setUp()
{
$this->useModelSet('cms');
parent::setUp();
}


/**
* @test
* @covers Doctrine\ORM\UnitOfWork::forgetEntity
*/
public function testForgetEntity()
{
$user = new CmsUser;
$user->username = 'bilouwan';
$user->name = 'moroine';
$hash = spl_object_hash($user);
$this->_em->persist($user);
$this->_em->flush();

$uow = $this->_em->getUnitOfWork();
$reflexion = new \ReflectionClass(get_class($uow));
$method = $reflexion->getMethod('forgetEntity');
$method->setAccessible(true);
$method->invoke($uow, $user);

$propertyNames = [
'entityInsertions',
'entityUpdates',
'entityDeletions',
'entityIdentifiers',
'entityStates',
'originalEntityData',
];

foreach ($propertyNames as $propertyName) {
$property = $reflexion->getProperty($propertyName);
$property->setAccessible(true);
$originalEntityData = $property->getValue($uow);

// As $user is unset, $hash could be reused by spl_object_hash
$this->assertArrayNotHasKey($hash, $originalEntityData);
}
}

/**
* @test
* @covers Doctrine\ORM\UnitOfWork::merge
*/
public function testMergeSplHashConflict()
{
// Create
$user = new CmsUser;
$hash = spl_object_hash($user);

$mergedUser = $this->_em->merge($user);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't self::assertSame($user, $mergedUser) here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should assert that $user and $mergedUser are two different object (by there reference) as merge operation should return a managed copy of input object but the input object has to remain unmanaged ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We couldn't assertSame as for object assertSame check if the two variables reference the same object. In the case of merge, it will return a copy so it's another object

unset($user);

$uow = $this->_em->getUnitOfWork();
$reflexion = new \ReflectionClass(get_class($uow));
$property = $reflexion->getProperty('originalEntityData');
$property->setAccessible(true);
$originalEntityData = $property->getValue($uow);

// As $user is unset, $hash could be reused by spl_object_hash
$this->assertArrayNotHasKey($hash, $originalEntityData);
876D Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The object still exists in memory here, since it is merged into the UoW.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of what I understood, the $user reference is never kept by the merge operation. As the bug that I uncounter is the fact my $user doesn't exists anymore and then I try to merge a new object but the uow kept old hash in their originalEntityData array.

}
}
0