From aa6ac3d6b00edfea0e2cf4cecffad221f7f9d221 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Tue, 28 Dec 2010 11:59:51 +0100 Subject: [PATCH] DDC-945 - Throw exception in ClassMetadataFactory when mapped superclass has to many associations. --- .../ORM/Mapping/ClassMetadataFactory.php | 3 ++ lib/Doctrine/ORM/Mapping/MappingException.php | 4 ++ .../ORM/Mapping/AnnotationDriverTest.php | 42 ++++++++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 174e95eaf..0f0fb17aa 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -382,6 +382,9 @@ class ClassMetadataFactory { foreach ($parentClass->associationMappings as $field => $mapping) { if ($parentClass->isMappedSuperclass) { + if ($mapping['type'] & ClassMetadata::TO_MANY) { + throw MappingException::illegalToManyAssocationOnMappedSuperclass($parentClass->name, $field); + } $mapping['sourceEntity'] = $subClass->name; } diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index 15cfc60ff..14bd9ca0b 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -227,4 +227,8 @@ class MappingException extends \Doctrine\ORM\ORMException return new self("Duplicate definition of column '".$columnName."' on entity '".$className."' in a field or discriminator column mapping."); } + public static function illegalToManyAssocationOnMappedSuperclass($className, $field) + { + return new self("It is illegal to put a one-to-many or many-to-many association on mapped superclass '".$className."#".$field."'."); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php index c2f3a13c4..5730fcfe4 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php @@ -125,14 +125,30 @@ class AnnotationDriverTest extends AbstractMappingDriverTest $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory(); $factory->setEntityManager($em); - $classPage = new ClassMetadata('Doctrine\Tests\Models\DirectoryTree\File'); $classPage = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\File'); $this->assertEquals('Doctrine\Tests\Models\DirectoryTree\File', $classPage->associationMappings['parentDirectory']['sourceEntity']); - $classDirectory = new ClassMetadata('Doctrine\Tests\Models\DirectoryTree\Directory'); $classDirectory = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\Directory'); $this->assertEquals('Doctrine\Tests\Models\DirectoryTree\Directory', $classDirectory->associationMappings['parentDirectory']['sourceEntity']); } + + /** + * @group DDC-945 + */ + public function testInvalidMappedSuperClassWithManyToManyAssociation() + { + $annotationDriver = $this->_loadDriver(); + + $em = $this->_getTestEntityManager(); + $em->getConfiguration()->setMetadataDriverImpl($annotationDriver); + $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory(); + $factory->setEntityManager($em); + + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', + "It is illegal to put a one-to-many or many-to-many association on ". + "mapped superclass 'Doctrine\Tests\ORM\Mapping\InvalidMappedSuperClass#users'"); + $usingInvalidMsc = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\UsingInvalidMappedSuperClass'); + } } /** @@ -143,3 +159,25 @@ class ColumnWithoutType /** @Id @Column */ public $id; } + +/** + * @MappedSuperclass + */ +class InvalidMappedSuperClass +{ + /** + * @ManyToMany(targetEntity="Doctrine\Tests\Models\CMS\CmsUser") + */ + private $users; +} + +/** + * @Entity + */ +class UsingInvalidMappedSuperClass extends InvalidMappedSuperClass +{ + /** + * @Id @Column(type="integer") @GeneratedValue + */ + private $id; +} \ No newline at end of file