Skip to content

Commit

Permalink
Fix notice in ClassMetadata when there is no ID Column defined
Browse files Browse the repository at this point in the history
When you forget about defining the ID/PK Column, then this ugly Notice appear.
Now it will throw nice Exception.
  • Loading branch information
JarJak authored and lcobucci committed Apr 30, 2017
1 parent 03972c9 commit 38bfcc6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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()
{
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/ORM/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/Doctrine/Tests/Models/DDC6412/DDC6412File.php
Original file line number Diff line number Diff line change
@@ -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;
}

10 changes: 10 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -209,6 +210,15 @@ public function testGetSingleIdentifierFieldName_MultipleIdentifierEntity_Throws
$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);
Expand Down

0 comments on commit 38bfcc6

Please sign in to comment.