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:
parent
03972c9c3a
commit
38bfcc6a7a
@ -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()
|
||||
{
|
||||
|
@ -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
|
||||
|
21
tests/Doctrine/Tests/Models/DDC6412/DDC6412File.php
Normal file
21
tests/Doctrine/Tests/Models/DDC6412/DDC6412File.php
Normal 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;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ use Doctrine\Tests\Models\Company\CompanyContract;
|
||||
use Doctrine\Tests\Models\CustomType\CustomTypeParent;
|
||||
use Doctrine\Tests\Models\DDC117\DDC117Article;
|
||||
use Doctrine\Tests\Models\DDC117\DDC117ArticleDetails;
|
||||
use Doctrine\Tests\Models\DDC6412\DDC6412File;
|
||||
use Doctrine\Tests\Models\DDC964\DDC964Admin;
|
||||
use Doctrine\Tests\Models\DDC964\DDC964Guest;
|
||||
use Doctrine\Tests\Models\Routing\RoutingLeg;
|
||||
@ -209,6 +210,15 @@ class ClassMetadataTest extends OrmTestCase
|
||||
$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()
|
||||
{
|
||||
$cm = new ClassMetadata(CMS\CmsUser::class);
|
||||
|
Loading…
x
Reference in New Issue
Block a user