From 10b70df1afb327933dc45221551b9b3d6d2757b7 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sat, 25 Jun 2011 10:20:37 +0200 Subject: [PATCH] DDC-1218, DDC-1156 - Fixed bugs with mapped superclasses in inheritance hierachies --- .../ORM/Mapping/ClassMetadataFactory.php | 8 +++- .../Mapping/BasicInheritanceMappingTest.php | 48 ++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index c036ba2f6..3f65da3c5 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -248,11 +248,13 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface // Move down the hierarchy of parent classes, starting from the topmost class $parent = null; + $rootEntityFound = false; $visited = array(); foreach ($parentClasses as $className) { if (isset($this->loadedMetadata[$className])) { $parent = $this->loadedMetadata[$className]; if ( ! $parent->isMappedSuperclass) { + $rootEntityFound = true; array_unshift($visited, $className); } continue; @@ -281,7 +283,10 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface throw MappingException::reflectionFailure($className, $e); } - if ($parent && ! $parent->isMappedSuperclass) { + // If this class has a parent the id generator strategy is inherited. + // However this is only true if the hierachy of parents contains the root entity, + // if it consinsts 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()) { @@ -336,6 +341,7 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface $parent = $class; if ( ! $class->isMappedSuperclass) { + $rootEntityFound = true; array_unshift($visited, $className); } diff --git a/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php b/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php index 033a2488b..91ebbd99b 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php @@ -113,6 +113,32 @@ class BasicInheritanceMappingTest extends \Doctrine\Tests\OrmTestCase $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator); $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition); } + + /** + * @group DDC-1156 + * @group DDC-1218 + */ + public function testSequenceDefinitionInHierachyWithSandwichMappedSuperclass() + { + $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyD'); + /* @var $class ClassMetadataInfo */ + + $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator); + $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition); + } + + /** + * @group DDC-1156 + * @group DDC-1218 + */ + public function testMultipleMappedSuperclasses() + { + $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\MediumSuperclassEntity'); + /* @var $class ClassMetadataInfo */ + + $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator); + $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition); + } } class TransientBaseClass { @@ -164,7 +190,8 @@ class EntitySubClass2 extends MappedSuperclassBase { abstract class HierachyBase { /** - * @Column(type="integer") @Id @GeneratedValue + * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE") + * @SequenceGenerator(sequenceName="foo", initialValue="10") * @var int */ public $id; @@ -229,8 +256,25 @@ class SuperclassEntity extends SuperclassBase abstract class SuperclassBase { /** - * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE") @SequenceGenerator(sequenceName="foo", initialValue="10") + * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE") + * @SequenceGenerator(sequenceName="foo", initialValue="10") * @var int */ public $id; +} + +/** + * @MappedSuperclass + */ +abstract class MediumSuperclassBase extends SuperclassBase +{ + +} + +/** + * @Entity + */ +class MediumSuperclassEntity extends MediumSuperclassBase +{ + } \ No newline at end of file