From 38bfcc6a7ad264b880b847b69cafa10e3df5cf5d Mon Sep 17 00:00:00 2001 From: Jarek Jakubowski Date: Tue, 25 Apr 2017 11:01:17 +0200 Subject: [PATCH] Fix notice in ClassMetadata when there is no ID Column defined When you forget about defining the ID/PK Column, then this ugly Notice appear. Now it will throw nice Exception. --- .../ORM/Mapping/ClassMetadataInfo.php | 8 +++++-- lib/Doctrine/ORM/Mapping/MappingException.php | 10 +++++++++ .../Tests/Models/DDC6412/DDC6412File.php | 21 +++++++++++++++++++ .../Tests/ORM/Mapping/ClassMetadataTest.php | 10 +++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/DDC6412/DDC6412File.php diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 9a581116391..ce3cd8685f8 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1796,13 +1796,17 @@ public function getIdentifierFieldNames() * * @return string * - * @throws MappingException If the class has a composite primary key. + * @throws MappingException If the class doesn't have an identifier or it has a composite primary key. */ public function getSingleIdentifierFieldName() { if ($this->isIdentifierComposite) { throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->name); } + + if ( ! isset($this->identifier[0])) { + throw MappingException::noIdDefined($this->name); + } return $this->identifier[0]; } @@ -1813,7 +1817,7 @@ public function getSingleIdentifierFieldName() * * @return string * - * @throws MappingException If the class has a composite primary key. + * @throws MappingException If the class doesn't have an identifier or it has a composite primary key. */ public function getSingleIdentifierColumnName() { diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index 97686d6544e..7dc4405159a 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -424,6 +424,16 @@ public static function singleIdNotAllowedOnCompositePrimaryKey($entity) return new self('Single id is not allowed on composite primary key in entity '.$entity); } + /** + * @param string $entity + * + * @return MappingException + */ + public static function noIdDefined($entity) + { + return new self('No ID defined for entity ' . $entity); + } + /** * @param string $entity * @param string $fieldName diff --git a/tests/Doctrine/Tests/Models/DDC6412/DDC6412File.php b/tests/Doctrine/Tests/Models/DDC6412/DDC6412File.php new file mode 100644 index 00000000000..64551cdf4fb --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC6412/DDC6412File.php @@ -0,0 +1,21 @@ +getSingleIdentifierFieldName(); } + public function testGetSingleIdentifierFieldName_NoIdEntity_ThrowsException() + { + $cm = new ClassMetadata(DDC6412File::class); + $cm->initializeReflection(new RuntimeReflectionService()); + + $this->expectException(\Doctrine\ORM\Mapping\MappingException::class); + $cm->getSingleIdentifierFieldName(); + } + public function testDuplicateAssociationMappingException() { $cm = new ClassMetadata(CMS\CmsUser::class);