From 62431ae4771c3714247685e724c9a77016465430 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 1/4] 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 79dcdb2cb..5670069a8 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -940,8 +940,13 @@ class ClassMetadataInfo implements ClassMetadata 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 000000000..c3b634abf --- /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 000000000..334dc2052 --- /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 000000000..da42d1c54 --- /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 000000000..e6415d393 --- /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)); + } +} From f181cf6c6b1aef0daaae37e2d13aadc924cd47c2 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Jun 2016 12:42:49 +0200 Subject: [PATCH 2/4] #5867 simplifying test case by inlining all required models into the test case --- .../Tests/Models/DDC3303/DDC3303Address.php | 60 ---------------- .../Tests/Models/DDC3303/DDC3303Employee.php | 39 ----------- .../Tests/Models/DDC3303/DDC3303Person.php | 61 ----------------- .../ORM/Functional/Ticket/DDC3303Test.php | 68 ++++++++++++++++--- 4 files changed, 60 insertions(+), 168 deletions(-) delete mode 100644 tests/Doctrine/Tests/Models/DDC3303/DDC3303Address.php delete mode 100644 tests/Doctrine/Tests/Models/DDC3303/DDC3303Employee.php delete mode 100644 tests/Doctrine/Tests/Models/DDC3303/DDC3303Person.php diff --git a/tests/Doctrine/Tests/Models/DDC3303/DDC3303Address.php b/tests/Doctrine/Tests/Models/DDC3303/DDC3303Address.php deleted file mode 100644 index c3b634abf..000000000 --- a/tests/Doctrine/Tests/Models/DDC3303/DDC3303Address.php +++ /dev/null @@ -1,60 +0,0 @@ -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 deleted file mode 100644 index 334dc2052..000000000 --- a/tests/Doctrine/Tests/Models/DDC3303/DDC3303Employee.php +++ /dev/null @@ -1,39 +0,0 @@ -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 deleted file mode 100644 index da42d1c54..000000000 --- a/tests/Doctrine/Tests/Models/DDC3303/DDC3303Person.php +++ /dev/null @@ -1,61 +0,0 @@ -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 index e6415d393..b122d9df6 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php @@ -1,18 +1,15 @@ _schemaTool->createSchema(array($this->_em->getClassMetadata(DDC3303Employee::class))); + parent::setUp(); + + $this->_schemaTool->createSchema([$this->_em->getClassMetadata(DDC3303Employee::class)]); } public function testEmbeddedObjectsAreAlsoInherited() @@ -27,6 +24,61 @@ class DDC3303Test extends OrmFunctionalTestCase $this->_em->flush(); $this->_em->clear(); - $this->assertEquals($employee, $this->_em->find(DDC3303Employee::class, 1)); + self::assertEquals($employee, $this->_em->find(DDC3303Employee::class, 'John Doe')); + } +} + +/** @MappedSuperclass */ +abstract class DDC3303Person +{ + /** @Id @GeneratedValue(strategy="NONE") @Column(type="string") @var string */ + private $name; + + /** @Embedded(class="DDC3303Address") @var DDC3303Address */ + private $address; + + public function __construct($name, DDC3303Address $address) + { + $this->name = $name; + $this->address = $address; + } +} + +/** + * @Embeddable + */ +class DDC3303Address +{ + /** @Column(type="string") @var string */ + private $street; + + /** @Column(type="integer") @var int */ + private $number; + + /** @Column(type="string") @var string */ + private $city; + + public function __construct($street, $number, $city) + { + $this->street = $street; + $this->number = $number; + $this->city = $city; + } +} + +/** + * @Entity + * @Table(name="ddc3303_employee") + */ +class DDC3303Employee extends DDC3303Person +{ + /** @Column(type="string") @var string */ + private $company; + + public function __construct($name, DDC3303Address $address, $company) + { + parent::__construct($name, $address); + + $this->company = $company; } } From 2af84c6025f8308e9e91cc356ab2409e5fa78974 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Jun 2016 12:44:19 +0200 Subject: [PATCH 3/4] #5867 `@group` annotations, describing scenario --- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php index b122d9df6..b40f99ff9 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php @@ -12,6 +12,13 @@ class DDC3303Test extends OrmFunctionalTestCase $this->_schemaTool->createSchema([$this->_em->getClassMetadata(DDC3303Employee::class)]); } + /** + * @group 4097 + * @group 4277 + * @group 5867 + * + * When using an embedded field in an inheritance, private properties should also be inherited. + */ public function testEmbeddedObjectsAreAlsoInherited() { $employee = new DDC3303Employee( From b183818fa8bed47a6cb190e9386625395c568be1 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Jun 2016 12:48:15 +0200 Subject: [PATCH 4/4] #5867 s/::class/::CLASSNAME for PHP 5.4 compat --- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php index b40f99ff9..df96bf901 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php @@ -9,7 +9,7 @@ class DDC3303Test extends OrmFunctionalTestCase { parent::setUp(); - $this->_schemaTool->createSchema([$this->_em->getClassMetadata(DDC3303Employee::class)]); + $this->_schemaTool->createSchema([$this->_em->getClassMetadata(DDC3303Employee::CLASSNAME)]); } /** @@ -31,7 +31,7 @@ class DDC3303Test extends OrmFunctionalTestCase $this->_em->flush(); $this->_em->clear(); - self::assertEquals($employee, $this->_em->find(DDC3303Employee::class, 'John Doe')); + self::assertEquals($employee, $this->_em->find(DDC3303Employee::CLASSNAME, 'John Doe')); } } @@ -79,6 +79,8 @@ class DDC3303Address */ class DDC3303Employee extends DDC3303Person { + const CLASSNAME = __CLASS__; + /** @Column(type="string") @var string */ private $company;