1
0
mirror of synced 2025-02-02 21:41:45 +03:00

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.
This commit is contained in:
Jarek Jakubowski 2017-04-25 11:01:17 +02:00 committed by Luís Cobucci
parent 03972c9c3a
commit 38bfcc6a7a
No known key found for this signature in database
GPG Key ID: EC61C5F01750ED3C
4 changed files with 47 additions and 2 deletions

View File

@ -1796,13 +1796,17 @@ class ClassMetadataInfo implements ClassMetadata
* *
* @return string * @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() public function getSingleIdentifierFieldName()
{ {
if ($this->isIdentifierComposite) { if ($this->isIdentifierComposite) {
throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->name); throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->name);
} }
if ( ! isset($this->identifier[0])) {
throw MappingException::noIdDefined($this->name);
}
return $this->identifier[0]; return $this->identifier[0];
} }
@ -1813,7 +1817,7 @@ class ClassMetadataInfo implements ClassMetadata
* *
* @return string * @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() public function getSingleIdentifierColumnName()
{ {

View File

@ -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); 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 $entity
* @param string $fieldName * @param string $fieldName

View File

@ -0,0 +1,21 @@
<?php
namespace Doctrine\Tests\Models\DDC6412;
/**
* @Entity
*/
class DDC6412File
{
/**
* @Column(type="integer")
* @GeneratedValue
*/
public $id;
/**
* @Column(length=50, name="file_name")
*/
public $name;
}

View File

@ -14,6 +14,7 @@ use Doctrine\Tests\Models\Company\CompanyContract;
use Doctrine\Tests\Models\CustomType\CustomTypeParent; use Doctrine\Tests\Models\CustomType\CustomTypeParent;
use Doctrine\Tests\Models\DDC117\DDC117Article; use Doctrine\Tests\Models\DDC117\DDC117Article;
use Doctrine\Tests\Models\DDC117\DDC117ArticleDetails; use Doctrine\Tests\Models\DDC117\DDC117ArticleDetails;
use Doctrine\Tests\Models\DDC6412\DDC6412File;
use Doctrine\Tests\Models\DDC964\DDC964Admin; use Doctrine\Tests\Models\DDC964\DDC964Admin;
use Doctrine\Tests\Models\DDC964\DDC964Guest; use Doctrine\Tests\Models\DDC964\DDC964Guest;
use Doctrine\Tests\Models\Routing\RoutingLeg; use Doctrine\Tests\Models\Routing\RoutingLeg;
@ -209,6 +210,15 @@ class ClassMetadataTest extends OrmTestCase
$cm->getSingleIdentifierFieldName(); $cm->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() public function testDuplicateAssociationMappingException()
{ {
$cm = new ClassMetadata(CMS\CmsUser::class); $cm = new ClassMetadata(CMS\CmsUser::class);