From 4d172e259134000560cf9e377598eef19f17ccb3 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Tue, 12 May 2020 20:10:26 +0200 Subject: [PATCH] Revert changes to embeddable mapping in 2.7 (#8138) * Revert "Fix inherited embeddables and nesting after AnnotationDriver change #8006 (#8036)" This reverts commit a9b6b720175fae69e90247e6b7a0e34d7da2f1cc. * Revert "Make Embeddable not transient" This reverts commit 58677c29b4bc97273da950f6312e43ff540a7407. * Housekeeping: CS fixes --- docs/en/tutorials/embeddables.rst | 4 +- .../ORM/Mapping/ClassMetadataFactory.php | 10 +- .../ORM/Mapping/Driver/AnnotationDriver.php | 3 - .../ORM/Functional/Ticket/GH8031Test.php | 163 ------------------ .../Tests/ORM/Functional/ValueObjectsTest.php | 5 - 5 files changed, 7 insertions(+), 178 deletions(-) delete mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/GH8031Test.php diff --git a/docs/en/tutorials/embeddables.rst b/docs/en/tutorials/embeddables.rst index c49bdfc65af..483e58d9da6 100644 --- a/docs/en/tutorials/embeddables.rst +++ b/docs/en/tutorials/embeddables.rst @@ -8,9 +8,7 @@ or address are the primary use case for this feature. .. note:: - Embeddables can not contain references to entities. They can however compose - other embeddables in addition to holding properties with basic ``@Column`` - mapping. + Embeddables can only contain properties with basic ``@Column`` mapping. For the purposes of this tutorial, we will assume that you have a ``User`` class in your application and you would like to store an address in diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index e75ae1f823d..d36ea308464 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -404,7 +404,7 @@ private function getShortName($className) private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) { foreach ($parentClass->fieldMappings as $mapping) { - if (! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass && ! $parentClass->isEmbeddedClass) { + if (! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { $mapping['inherited'] = $parentClass->name; } if (! isset($mapping['declared'])) { @@ -472,6 +472,10 @@ private function addInheritedEmbeddedClasses(ClassMetadata $subClass, ClassMetad private function addNestedEmbeddedClasses(ClassMetadata $subClass, ClassMetadata $parentClass, $prefix) { foreach ($subClass->embeddedClasses as $property => $embeddableClass) { + if (isset($embeddableClass['inherited'])) { + continue; + } + $embeddableMetadata = $this->getMetadataFor($embeddableClass['class']); $parentClass->mapEmbedded( @@ -779,9 +783,7 @@ protected function getDriver() */ protected function isEntity(ClassMetadataInterface $class) { - assert($class instanceof ClassMetadata); - - return $class->isMappedSuperclass === false && $class->isEmbeddedClass === false; + return isset($class->isMappedSuperclass) && $class->isMappedSuperclass === false; } /** diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index afa7ad614db..6e93c6e03f8 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -45,7 +45,6 @@ class AnnotationDriver extends AbstractAnnotationDriver protected $entityAnnotationClasses = [ Mapping\Entity::class => 1, Mapping\MappedSuperclass::class => 2, - Mapping\Embeddable::class => 3, ]; /** @@ -278,8 +277,6 @@ public function loadMetadataForClass($className, ClassMetadata $metadata) /* @var $property \ReflectionProperty */ foreach ($class->getProperties() as $property) { if ($metadata->isMappedSuperclass && ! $property->isPrivate() - || - $metadata->isEmbeddedClass && $property->getDeclaringClass()->getName() !== $class->getName() || $metadata->isInheritedField($property->name) || diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH8031Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH8031Test.php deleted file mode 100644 index 7ae17d3f2f8..00000000000 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH8031Test.php +++ /dev/null @@ -1,163 +0,0 @@ -setUpEntitySchema([ - GH8031Invoice::class, - ]); - } - - public function testEntityIsFetched() - { - $entity = new GH8031Invoice(new GH8031InvoiceCode(1, 2020, new GH8031Nested(10))); - $this->_em->persist($entity); - $this->_em->flush(); - $this->_em->clear(); - - /** @var GH8031Invoice $fetched */ - $fetched = $this->_em->find(GH8031Invoice::class, $entity->getId()); - $this->assertInstanceOf(GH8031Invoice::class, $fetched); - $this->assertSame(1, $fetched->getCode()->getNumber()); - $this->assertSame(2020, $fetched->getCode()->getYear()); - - $this->_em->clear(); - $this->assertCount( - 1, - $this->_em->getRepository(GH8031Invoice::class)->findBy([], ['code.number' => 'ASC']) - ); - } - - public function testEmbeddableWithAssociationNotAllowed() - { - $cm = $this->_em->getClassMetadata(GH8031EmbeddableWithAssociation::class); - - $this->assertArrayHasKey('invoice', $cm->associationMappings); - - $cm = $this->_em->getClassMetadata(GH8031Invoice::class); - - $this->assertCount(0, $cm->associationMappings); - } -} - -/** - * @Embeddable - */ -class GH8031EmbeddableWithAssociation -{ - /** @ManyToOne(targetEntity=GH8031Invoice::class) */ - public $invoice; -} - -/** - * @Embeddable - */ -class GH8031Nested -{ - /** - * @Column(type="integer", name="number", length=6) - * @var int - */ - protected $number; - - public function __construct(int $number) - { - $this->number = $number; - } - - public function getNumber() : int - { - return $this->number; - } -} - -/** - * @Embeddable - */ -class GH8031InvoiceCode extends GH8031AbstractYearSequenceValue -{ -} - -/** - * @Embeddable - */ -abstract class GH8031AbstractYearSequenceValue -{ - /** - * @Column(type="integer", name="number", length=6) - * @var int - */ - protected $number; - - /** - * @Column(type="smallint", name="year", length=4) - * @var int - */ - protected $year; - - /** @Embedded(class=GH8031Nested::class) */ - protected $nested; - - public function __construct(int $number, int $year, GH8031Nested $nested) - { - $this->number = $number; - $this->year = $year; - $this->nested = $nested; - } - - public function getNumber() : int - { - return $this->number; - } - - public function getYear() : int - { - return $this->year; - } -} - -/** - * @Entity - */ -class GH8031Invoice -{ - /** - * @Id - * @GeneratedValue - * @Column(type="integer") - */ - private $id; - - /** - * @Embedded(class=GH8031InvoiceCode::class) - * @var GH8031InvoiceCode - */ - private $code; - - /** @Embedded(class=GH8031EmbeddableWithAssociation::class) */ - private $embeddedAssoc; - - public function __construct(GH8031InvoiceCode $code) - { - $this->code = $code; - } - - public function getId() - { - return $this->id; - } - - public function getCode() : GH8031InvoiceCode - { - return $this->code; - } -} diff --git a/tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php b/tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php index f78b313e78d..7a483ebc783 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php @@ -328,11 +328,6 @@ public function getInfiniteEmbeddableNestingData() ['DDCNestingEmbeddable1', 'DDCNestingEmbeddable4'], ]; } - - public function testEmbeddableIsNotTransient() - { - $this->assertFalse($this->_em->getMetadataFactory()->isTransient(DDC93Address::class)); - } }