From bcc7bb1c9caaa8c2faae4a47d2ee4cd6bee59268 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sat, 6 Aug 2011 20:23:47 +0200 Subject: [PATCH] DDC-1300 - Fix bug in fetch join hydration of entities with foreign key identifier --- .../ORM/Internal/Hydration/ObjectHydrator.php | 10 +- .../ORM/Functional/Ticket/DDC1300Test.php | 108 ++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1300Test.php diff --git a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php index 2935f67a7..bb11a7431 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php @@ -216,13 +216,21 @@ class ObjectHydrator extends AbstractHydrator private function _getEntityFromIdentityMap($className, array $data) { + // TODO: Abstract this code and UnitOfWork::createEntity() equivalent? $class = $this->_ce[$className]; + /* @var $class ClassMetadata */ if ($class->isIdentifierComposite) { $idHash = ''; foreach ($class->identifier as $fieldName) { - $idHash .= $data[$fieldName] . ' '; + if (isset($class->associationMappings[$fieldName])) { + $idHash .= $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']] . ' '; + } else { + $idHash .= $data[$fieldName] . ' '; + } } return $this->_uow->tryGetByIdHash(rtrim($idHash), $class->rootEntityName); + } else if (isset($class->associationMappings[$class->identifier[0]])) { + return $this->_uow->tryGetByIdHash($data[$class->associationMappings[$class->identifier[0]]['joinColumns'][0]['name']], $class->rootEntityName); } else { return $this->_uow->tryGetByIdHash($data[$class->identifier[0]], $class->rootEntityName); } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1300Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1300Test.php new file mode 100644 index 000000000..38278dc1c --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1300Test.php @@ -0,0 +1,108 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1300Foo'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1300FooLocale'), + )); + } + + public function testIssue() + { + $foo = new DDC1300Foo(); + $foo->_fooReference = "foo"; + + $this->_em->persist($foo); + $this->_em->flush(); + + $locale = new DDC1300FooLocale(); + $locale->_foo = $foo; + $locale->_locale = "en"; + $locale->_title = "blub"; + + $this->_em->persist($locale); + $this->_em->flush(); + + $query = $this->_em->createQuery('SELECT f, fl FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1300Foo f JOIN f._fooLocaleRefFoo fl'); + $result = $query->getResult(); + + $this->assertEquals(1, count($result)); + } +} + +/** + * @Entity + */ +class DDC1300Foo +{ + /** + * @var integer fooID + * @Column(name="fooID", type="integer", nullable=false) + * @GeneratedValue(strategy="AUTO") + * @Id + */ + public $_fooID = null; + + /** + * @var string fooReference + * @Column(name="fooReference", type="string", nullable=true, length=45) + */ + public $_fooReference = null; + + /** + * @OneToMany(targetEntity="DDC1300FooLocale", mappedBy="_foo", + * cascade={"persist"}) + */ + public $_fooLocaleRefFoo = null; + + /** + * Constructor + * + * @param array|Zend_Config|null $options + * @return Bug_Model_Foo + */ + public function __construct($options = null) + { + $this->_fooLocaleRefFoo = new \Doctrine\Common\Collections\ArrayCollection(); + } + +} + +/** + * @Entity + */ +class DDC1300FooLocale +{ + + /** + * @ManyToOne(targetEntity="DDC1300Foo") + * @JoinColumn(name="fooID", referencedColumnName="fooID") + * @Id + */ + public $_foo = null; + + /** + * @var string locale + * @Column(name="locale", type="string", nullable=false, length=5) + * @Id + */ + public $_locale = null; + + /** + * @var string title + * @Column(name="title", type="string", nullable=true, length=150) + */ + public $_title = null; + +} \ No newline at end of file