From cdde6e8a5c32cf403b84e7898a7c0a8f0230a4c7 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Mon, 13 Feb 2012 23:38:36 -0200 Subject: [PATCH 1/2] fix required discriminator column name --- lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php | 9 ++++++--- lib/Doctrine/ORM/Mapping/MappingException.php | 5 +++++ .../Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php | 11 +++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 253ab6003..725ccfaed 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1937,19 +1937,22 @@ class ClassMetadataInfo implements ClassMetadata public function setDiscriminatorColumn($columnDef) { if ($columnDef !== null) { + if ( ! isset($columnDef['name'])) { + throw MappingException::nameIsMandatoryForDiscriminatorColumns($this->name, $columnDef); + } + if (isset($this->fieldNames[$columnDef['name']])) { throw MappingException::duplicateColumnName($this->name, $columnDef['name']); } - if ( ! isset($columnDef['name'])) { - throw MappingException::nameIsMandatoryForDiscriminatorColumns($this->name, $columnDef); - } if ( ! isset($columnDef['fieldName'])) { $columnDef['fieldName'] = $columnDef['name']; } + if ( ! isset($columnDef['type'])) { $columnDef['type'] = "string"; } + if (in_array($columnDef['type'], array("boolean", "array", "object", "datetime", "time", "date"))) { throw MappingException::invalidDiscriminatorColumnType($this->name, $columnDef['type']); } diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index c71c2e91c..0eff17cd6 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -226,6 +226,11 @@ class MappingException extends \Doctrine\ORM\ORMException return new self("Discriminator column type on entity class '$className' is not allowed to be '$type'. 'string' or 'integer' type variables are suggested!"); } + public static function nameIsMandatoryForDiscriminatorColumns($className) + { + return new self("Discriminator column name on entity class '$className' is not defined."); + } + public static function cannotVersionIdField($className, $fieldName) { return new self("Setting Id field '$fieldName' as versionale in entity class '$className' is not supported."); diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index c16eedba9..8f079e789 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -596,6 +596,17 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase $cm->validateAssocations(); } + /** + * @expectedException \Doctrine\ORM\Mapping\MappingException + * @expectedExceptionMessage Discriminator column name on entity class 'Doctrine\Tests\Models\CMS\CmsUser' is not defined. + */ + public function testNameIsMandatoryForDiscriminatorColumnsMappingException() + { + $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService); + $cm->setDiscriminatorColumn(array()); + } + /** * @group DDC-984 * @group DDC-559 From da9b2e805e5185c47fc4806755406091b46fd601 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Mon, 13 Feb 2012 23:43:19 -0200 Subject: [PATCH 2/2] remove unused parameter --- lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 725ccfaed..0c28ae473 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1938,7 +1938,7 @@ class ClassMetadataInfo implements ClassMetadata { if ($columnDef !== null) { if ( ! isset($columnDef['name'])) { - throw MappingException::nameIsMandatoryForDiscriminatorColumns($this->name, $columnDef); + throw MappingException::nameIsMandatoryForDiscriminatorColumns($this->name); } if (isset($this->fieldNames[$columnDef['name']])) {