From 86c81da7ce403b178c6acdbdc2767e5721804bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steve=20M=C3=BCller?= Date: Thu, 19 Nov 2015 16:10:48 +0100 Subject: [PATCH] inherit ID generator strategy mapping from embeddables --- .../ORM/Mapping/ClassMetadataFactory.php | 41 +++++++++++++------ .../Tests/Models/DDC4006/DDC4006User.php | 14 +++++++ .../Tests/Models/DDC4006/DDC4006UserId.php | 16 ++++++++ .../ORM/Mapping/ClassMetadataFactoryTest.php | 21 ++++++++-- 4 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/DDC4006/DDC4006User.php create mode 100644 tests/Doctrine/Tests/Models/DDC4006/DDC4006UserId.php diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index e0eacfa53..b8a10ed77 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -157,19 +157,7 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory // However this is only true if the hierarchy of parents contains the root entity, // if it consists of mapped superclasses these don't necessarily include the id field. if ($parent && $rootEntityFound) { - if ($parent->isIdGeneratorSequence()) { - $class->setSequenceGeneratorDefinition($parent->sequenceGeneratorDefinition); - } else if ($parent->isIdGeneratorTable()) { - $class->tableGeneratorDefinition = $parent->tableGeneratorDefinition; - } - - if ($parent->generatorType) { - $class->setIdGeneratorType($parent->generatorType); - } - - if ($parent->idGenerator) { - $class->setIdGenerator($parent->idGenerator); - } + $this->inheritIdGeneratorMapping($class, $parent); } else { $this->completeIdGeneratorMapping($class); } @@ -197,6 +185,10 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory $this->addNestedEmbeddedClasses($embeddableMetadata, $class, $property); } + if (! empty($embeddableMetadata->getIdentifier())) { + $this->inheritIdGeneratorMapping($class, $embeddableMetadata); + } + $class->inlineEmbeddable($property, $embeddableMetadata); unset($this->embeddablesActiveNesting[$class->name]); @@ -712,6 +704,29 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory } } + /** + * Inherits the ID generator mapping from a parent class. + * + * @param ClassMetadataInfo $class + * @param ClassMetadataInfo $parent + */ + private function inheritIdGeneratorMapping(ClassMetadataInfo $class, ClassMetadataInfo $parent) + { + if ($parent->isIdGeneratorSequence()) { + $class->setSequenceGeneratorDefinition($parent->sequenceGeneratorDefinition); + } elseif ($parent->isIdGeneratorTable()) { + $class->tableGeneratorDefinition = $parent->tableGeneratorDefinition; + } + + if ($parent->generatorType) { + $class->setIdGeneratorType($parent->generatorType); + } + + if ($parent->idGenerator) { + $class->setIdGenerator($parent->idGenerator); + } + } + /** * {@inheritDoc} */ diff --git a/tests/Doctrine/Tests/Models/DDC4006/DDC4006User.php b/tests/Doctrine/Tests/Models/DDC4006/DDC4006User.php new file mode 100644 index 000000000..e5a937526 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC4006/DDC4006User.php @@ -0,0 +1,14 @@ +assertEquals('parent-id', $user['joinColumns'][0]['name']); $this->assertEquals('group-id', $user['joinColumns'][0]['referencedColumnName']); - + // Address Class Metadata $this->assertTrue($addressMetadata->fieldMappings['id']['quoted']); $this->assertTrue($addressMetadata->fieldMappings['zip']['quoted']); @@ -327,11 +327,11 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase // User Class Metadata $this->assertTrue($userMetadata->fieldMappings['id']['quoted']); $this->assertTrue($userMetadata->fieldMappings['name']['quoted']); - + $this->assertEquals('user-id', $userMetadata->fieldMappings['id']['columnName']); $this->assertEquals('user-name', $userMetadata->fieldMappings['name']['columnName']); - + $address = $userMetadata->associationMappings['address']; $this->assertTrue($address['joinColumns'][0]['quoted']); $this->assertEquals('address-id', $address['joinColumns'][0]['name']); @@ -423,6 +423,21 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase $cmf->getMetadataFor($metadata->name); } + + /** + * @group DDC-4006 + */ + public function testInheritsIdGeneratorMappingFromEmbeddable() + { + $cmf = new ClassMetadataFactory(); + $driver = $this->createAnnotationDriver(array(__DIR__ . '/../../Models/DDC4006/')); + $em = $this->_createEntityManager($driver); + $cmf->setEntityManager($em); + + $userMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\DDC4006\DDC4006User'); + + $this->assertTrue($userMetadata->isIdGeneratorIdentity()); + } } /* Test subject class with overridden factory method for mocking purposes */