From fd44894e9a73587457c66f141ff53260420cb7d8 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 23 Jan 2011 14:20:15 +0100 Subject: [PATCH] DDC-996 - Throw more useful exception if fieldName is empty in a mapped field or association. --- lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php | 8 ++++---- lib/Doctrine/ORM/Mapping/MappingException.php | 4 ++-- .../Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php | 11 +++++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 4ac7e87c0..353f47128 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -658,8 +658,8 @@ class ClassMetadataInfo protected function _validateAndCompleteFieldMapping(array &$mapping) { // Check mandatory fields - if ( ! isset($mapping['fieldName'])) { - throw MappingException::missingFieldName($this->name, $mapping); + if ( ! isset($mapping['fieldName']) || strlen($mapping['fieldName']) == 0) { + throw MappingException::missingFieldName($this->name); } if ( ! isset($mapping['type'])) { // Default to string @@ -749,8 +749,8 @@ class ClassMetadataInfo // Mandatory attributes for both sides // Mandatory: fieldName, targetEntity - if ( ! isset($mapping['fieldName'])) { - throw MappingException::missingFieldName(); + if ( ! isset($mapping['fieldName']) || strlen($mapping['fieldName']) == 0) { + throw MappingException::missingFieldName($this->name); } if ( ! isset($mapping['targetEntity'])) { throw MappingException::missingTargetEntity($mapping['fieldName']); diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index c8bc52771..11e35808c 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -48,9 +48,9 @@ class MappingException extends \Doctrine\ORM\ORMException return new self("Id generators can't be used with a composite id."); } - public static function missingFieldName() + public static function missingFieldName($entity) { - return new self("The association mapping misses the 'fieldName' attribute."); + return new self("The field or association mapping misses the 'fieldName' attribute in entity '$entity'."); } public static function missingTargetEntity($fieldName) diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index 1de9b5885..f618f6b3a 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -379,4 +379,15 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase 'joinColumns' => array(), )); } + + /** + * @group DDC-996 + */ + public function testEmptyFieldNameThrowsException() + { + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', + "The field or association mapping misses the 'fieldName' attribute in entity 'Doctrine\Tests\Models\CMS\CmsUser'."); + $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + $cm->mapField(array('fieldName' => '')); + } }