From 502585bf40f37233532594a707dbbbf0426a882e Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Mon, 20 Feb 2012 15:34:02 +0100 Subject: [PATCH] [DDC-1649] Add additional check for not allowed mapping of dependent association keys. --- lib/Doctrine/ORM/Tools/SchemaValidator.php | 5 +++ .../Tests/ORM/Tools/SchemaValidatorTest.php | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/lib/Doctrine/ORM/Tools/SchemaValidator.php b/lib/Doctrine/ORM/Tools/SchemaValidator.php index 0e1f80cdf..e98372d4c 100644 --- a/lib/Doctrine/ORM/Tools/SchemaValidator.php +++ b/lib/Doctrine/ORM/Tools/SchemaValidator.php @@ -106,6 +106,11 @@ class SchemaValidator $targetMetadata = $cmf->getMetadataFor($assoc['targetEntity']); + if ($assoc['id'] && $targetMetadata->containsForeignIdentifier) { + $ce[] = "Cannot map association '" . $class->name. "#". $fieldName ." as identifier, because " . + "the target entity '". $targetMetadata->name . "' also maps an association as identifier."; + } + /* @var $assoc AssociationMapping */ if ($assoc['mappedBy']) { if ($targetMetadata->hasField($assoc['mappedBy'])) { diff --git a/tests/Doctrine/Tests/ORM/Tools/SchemaValidatorTest.php b/tests/Doctrine/Tests/ORM/Tools/SchemaValidatorTest.php index 0b9657fe0..6f2e91236 100644 --- a/tests/Doctrine/Tests/ORM/Tools/SchemaValidatorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/SchemaValidatorTest.php @@ -122,6 +122,20 @@ class SchemaValidatorTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals(array(), $ce); } + + /** + * @group DDC-1649 + */ + public function testInvalidTripleAssociationAsKeyMapping() + { + $classThree = $this->em->getClassMetadata(__NAMESPACE__ . '\DDC1649Three'); + $ce = $this->validator->validateClass($classThree); + + $this->assertEquals(Array( + "Cannot map association 'Doctrine\Tests\ORM\Tools\DDC1649Three#two as identifier, because the target entity 'Doctrine\Tests\ORM\Tools\DDC1649Two' also maps an association as identifier.", + "The referenced column name 'id' has to be a primary key column on the target entity class 'Doctrine\Tests\ORM\Tools\DDC1649Two'." + ), $ce); + } } /** @@ -221,3 +235,33 @@ class DDC1587ValidEntity2 private $num; } +/** + * @Entity + */ +class DDC1649One +{ + /** + * @Id @Column @GeneratedValue + */ + public $id; +} + +/** + * @Entity + */ +class DDC1649Two +{ + /** @Id @ManyToOne(targetEntity="DDC1649One")@JoinColumn(name="id", referencedColumnName="id") */ + public $one; +} + +/** + * @Entity + */ +class DDC1649Three +{ + /** @Id @ManyToOne(targetEntity="DDC1649Two") @JoinColumn(name="id", + * referencedColumnName="id") */ + private $two; +} +