From 7f8a5f113c76f139800d6275216887b938afbc95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Thu, 9 Jun 2016 19:46:37 +0000 Subject: [PATCH] Allow the usage of embedded objects on parent classes. The `ClassMetadataInfo` was always using the "current class" to fetch the reflection of a property even when a field is declared on the parent class (which causes `ReflectionProperty` to throw an exception). --- .../ORM/Mapping/ClassMetadataInfo.php | 9 ++- .../Tests/Models/DDC3303/DDC3303Address.php | 60 ++++++++++++++++++ .../Tests/Models/DDC3303/DDC3303Employee.php | 39 ++++++++++++ .../Tests/Models/DDC3303/DDC3303Person.php | 61 +++++++++++++++++++ .../ORM/Functional/Ticket/DDC3303Test.php | 32 ++++++++++ 5 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/DDC3303/DDC3303Address.php create mode 100644 tests/Doctrine/Tests/Models/DDC3303/DDC3303Employee.php create mode 100644 tests/Doctrine/Tests/Models/DDC3303/DDC3303Person.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 90db1f8a6fa..5fa8f9e07dd 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -941,8 +941,13 @@ public function wakeupReflection($reflService) continue; } - $parentReflFields[$property] = $reflService->getAccessibleProperty($this->name, $property); - $this->reflFields[$property] = $reflService->getAccessibleProperty($this->name, $property); + $fieldRefl = $reflService->getAccessibleProperty( + isset($embeddedClass['declared']) ? $embeddedClass['declared'] : $this->name, + $property + ); + + $parentReflFields[$property] = $fieldRefl; + $this->reflFields[$property] = $fieldRefl; } foreach ($this->fieldMappings as $field => $mapping) { diff --git a/tests/Doctrine/Tests/Models/DDC3303/DDC3303Address.php b/tests/Doctrine/Tests/Models/DDC3303/DDC3303Address.php new file mode 100644 index 00000000000..c3b634abf6c --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC3303/DDC3303Address.php @@ -0,0 +1,60 @@ +street = $street; + $this->number = $number; + $this->city = $city; + } + + /** + * @return string + */ + public function getStreet() + { + return $this->street; + } + + /** + * @return mixed + */ + public function getNumber() + { + return $this->number; + } + + /** + * @return string + */ + public function getCity() + { + return $this->city; + } +} diff --git a/tests/Doctrine/Tests/Models/DDC3303/DDC3303Employee.php b/tests/Doctrine/Tests/Models/DDC3303/DDC3303Employee.php new file mode 100644 index 00000000000..334dc20529b --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC3303/DDC3303Employee.php @@ -0,0 +1,39 @@ +company = $company; + } + + /** + * @return string + */ + public function getCompany() + { + return $this->company; + } + + /** + * @param string $company + */ + public function setCompany($company) + { + $this->company = $company; + } +} diff --git a/tests/Doctrine/Tests/Models/DDC3303/DDC3303Person.php b/tests/Doctrine/Tests/Models/DDC3303/DDC3303Person.php new file mode 100644 index 00000000000..da42d1c5446 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC3303/DDC3303Person.php @@ -0,0 +1,61 @@ +name = $name; + $this->address = $address; + } + + /** + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @return DDC3303Address + */ + public function getAddress() + { + return $this->address; + } +} diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php new file mode 100644 index 00000000000..e6415d3937a --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php @@ -0,0 +1,32 @@ +_schemaTool->createSchema(array($this->_em->getClassMetadata(DDC3303Employee::class))); + } + + public function testEmbeddedObjectsAreAlsoInherited() + { + $employee = new DDC3303Employee( + 'John Doe', + new DDC3303Address('Somewhere', 123, 'Over the rainbow'), + 'Doctrine Inc' + ); + + $this->_em->persist($employee); + $this->_em->flush(); + $this->_em->clear(); + + $this->assertEquals($employee, $this->_em->find(DDC3303Employee::class, 1)); + } +}