From 640a8e58c74bc0ed3b24b8c037f731f4dd2c4c14 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Wed, 1 May 2013 18:46:41 +0200 Subject: [PATCH] [DDC-2106] Fix entity as parameter value when its managed but not yet with identifier. --- .../ORM/Persisters/BasicEntityPersister.php | 11 +--- lib/Doctrine/ORM/UnitOfWork.php | 2 +- .../ORM/Functional/Ticket/DDC2106Test.php | 64 +++++++++++++++++++ 3 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2106Test.php diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 176eb38f8..8d2aa5236 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -1848,16 +1848,7 @@ class BasicEntityPersister return $value; } - if ($this->em->getUnitOfWork()->getEntityState($value) === UnitOfWork::STATE_MANAGED) { - $idValues = $this->em->getUnitOfWork()->getEntityIdentifier($value); - - return reset($idValues); - } - - $class = $this->em->getClassMetadata(get_class($value)); - $idValues = $class->getIdentifierValues($value); - - return reset($idValues); + return $this->em->getUnitOfWork()->getSingleIdentifierValue($value); } /** diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index c1065dba2..498c4a699 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -2860,7 +2860,7 @@ class UnitOfWork implements PropertyChangedListener throw ORMInvalidArgumentException::invalidCompositeIdentifier(); } - $values = ($this->getEntityState($entity) === UnitOfWork::STATE_MANAGED) + $values = $this->isInIdentityMap($entity) ? $this->getEntityIdentifier($entity) : $class->getIdentifierValues($entity); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2106Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2106Test.php new file mode 100644 index 000000000..0de386f9a --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2106Test.php @@ -0,0 +1,64 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2106Entity'), + )); + } + + public function testDetachedEntityAsId() + { + // We want an uninitialized PersistentCollection $entity->children + $entity = new DDC2106Entity(); + $this->_em->persist($entity); + $this->_em->flush(); + $this->_em->detach($entity); + $entity = $this->_em->getRepository(__NAMESPACE__ . '\DDC2106Entity')->findOneBy(array()); + + // ... and a managed entity without id + $entityWithoutId = new DDC2106Entity(); + $this->_em->persist($entityWithoutId); + + $criteria = Criteria::create()->where(Criteria::expr()->eq('parent', $entityWithoutId)); + $entity->children->matching($criteria)->count(); + } +} + +/** + * @Entity + */ +class DDC2106Entity +{ + /** + * @Id + * @GeneratedValue(strategy="IDENTITY") + * @Column(type="integer") + */ + public $id; + + /** @ManyToOne(targetEntity="DDC2106Entity", inversedBy="children") */ + public $parent; + + /** + * @OneToMany(targetEntity="DDC2106Entity", mappedBy="parent", cascade={"persist"}) + */ + public $children; + + public function __construct() + { + $this->children = new \Doctrine\Common\Collections\ArrayCollection; + } +} +