Skip to content

Commit

Permalink
Merge pull request #201 from aramalipoor/patch-1
Browse files Browse the repository at this point in the history
Use UnitOfWork->contains() instead of isInIdentityMap when Phpcr document
  • Loading branch information
guilhermeblanco committed Sep 2, 2015
2 parents 562a342 + b424cc4 commit 85a14f3
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions lib/Doctrine/Common/DataFixtures/ReferenceRepository.php
Expand Up @@ -20,6 +20,7 @@
namespace Doctrine\Common\DataFixtures;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ODM\PHPCR\DocumentManager as PhpcrDocumentManager;

/**
* ReferenceRepository class manages references for
Expand Down Expand Up @@ -75,7 +76,7 @@ public function __construct(ObjectManager $manager)
protected function getIdentifier($reference, $uow)
{
// In case Reference is not yet managed in UnitOfWork
if ( ! $uow->isInIdentityMap($reference)) {
if ( ! $this->hasIdentifier($reference)) {
$class = $this->manager->getClassMetadata(get_class($reference));

return $class->getIdentifierValues($reference);
Expand All @@ -101,9 +102,10 @@ protected function getIdentifier($reference, $uow)
public function setReference($name, $reference)
{
$this->references[$name] = $reference;
// in case if reference is set after flush, store its identity
$uow = $this->manager->getUnitOfWork();
if ($uow->isInIdentityMap($reference)) {

if ($this->hasIdentifier($reference)) {
// in case if reference is set after flush, store its identity
$uow = $this->manager->getUnitOfWork();
$this->identities[$name] = $this->getIdentifier($reference, $uow);
}
}
Expand Down Expand Up @@ -158,14 +160,15 @@ public function getReference($name)

$reference = $this->references[$name];
$meta = $this->manager->getClassMetadata(get_class($reference));
$uow = $this->manager->getUnitOfWork();
if (!$uow->isInIdentityMap($reference) && isset($this->identities[$name])) {

if (!$this->manager->contains($reference) && isset($this->identities[$name])) {
$reference = $this->manager->getReference(
$meta->name,
$this->identities[$name]
);
$this->references[$name] = $reference; // already in identity map
}

return $reference;
}

Expand Down Expand Up @@ -232,4 +235,23 @@ public function getManager()
{
return $this->manager;
}

/**
* Checks if object has identifier already in unit of work.
*
* @param $reference
*
* @return bool
*/
private function hasIdentifier($reference)
{
// in case if reference is set after flush, store its identity
$uow = $this->manager->getUnitOfWork();

if ($this->manager instanceof PhpcrDocumentManager) {
return $uow->contains($reference);
} else {
return $uow->isInIdentityMap($reference);
}
}
}

0 comments on commit 85a14f3

Please sign in to comment.