From 251247c16f8c602e3565e41233b3aee128c948a3 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 8 Aug 2010 11:05:30 +0200 Subject: [PATCH] DDC-633 - Fix Eager ManyToOne or OneToOne relations being replaced by a proxy instead. --- lib/Doctrine/ORM/UnitOfWork.php | 8 ++- .../ORM/Functional/Ticket/DDC633Test.php | 69 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC633Test.php diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 2008cbe53..2d8cd1885 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1912,7 +1912,13 @@ class UnitOfWork implements PropertyChangedListener // If it might be a subtype, it can not be lazy $newValue = $assoc->load($entity, null, $this->em, $associatedId); } else { - $newValue = $this->em->getProxyFactory()->getProxy($assoc->targetEntityName, $associatedId); + if ($assoc->fetchMode == Mapping\AssociationMapping::FETCH_EAGER) { + // TODO: Maybe it could be optimized to do an eager fetch with a JOIN inside + // the persister instead of this rather unperformant approach. + $newValue = $this->em->find($assoc->targetEntityName, $associatedId); + } else { + $newValue = $this->em->getProxyFactory()->getProxy($assoc->targetEntityName, $associatedId); + } // PERF: Inlined & optimized code from UnitOfWork#registerManaged() $newValueOid = spl_object_hash($newValue); $this->entityIdentifiers[$newValueOid] = $associatedId; diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC633Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC633Test.php new file mode 100644 index 000000000..d375f408c --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC633Test.php @@ -0,0 +1,69 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC633Patient'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC633Appointment'), + )); + } catch(\Exception $e) { + + } + } + + public function testOneToOneEager() + { + $app = new DDC633Appointment(); + $pat = new DDC633Patient(); + $app->patient = $pat; + $pat->appointment = $app; + + $this->_em->persist($app); + $this->_em->persist($pat); + $this->_em->flush(); + $this->_em->clear(); + + $eagerAppointment = $this->_em->find(__NAMESPACE__ . '\DDC633Appointment', $app->id); + + $this->assertNotType('Doctrine\ORM\Proxy\Proxy', $eagerAppointment->patient); + } +} + +/** + * @Entity + */ +class DDC633Appointment +{ + /** @Id @Column(type="integer") @GeneratedValue */ + public $id; + + /** + * @OneToOne(targetEntity="DDC633Patient", inversedBy="appointment", fetch="EAGER") + */ + public $patient; + +} + +/** + * @Entity + */ +class DDC633Patient +{ + /** @Id @Column(type="integer") @GeneratedValue */ + public $id; + + /** + * @OneToOne(targetEntity="DDC633Appointment", mappedBy="patient") + */ + public $appointment; +} \ No newline at end of file