diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 9a5811163..ce3cd8685 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1796,13 +1796,17 @@ class ClassMetadataInfo implements ClassMetadata * * @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 @@ class ClassMetadataInfo implements ClassMetadata * * @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 97686d654..7dc440515 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -424,6 +424,16 @@ class MappingException extends \Doctrine\ORM\ORMException 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 000000000..64551cdf4 --- /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);