From 841d12e9b6efebc08a76687c94e963376d223444 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Sun, 20 Nov 2011 19:50:51 +0100 Subject: [PATCH] Move check for conversion SQL to ClassMetadataInfo --- lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php | 11 ++++++++++- lib/Doctrine/ORM/Mapping/MappingException.php | 5 +++++ .../AbstractEntityInheritancePersister.php | 4 ++-- .../ORM/Persisters/BasicEntityPersister.php | 11 ++++++----- lib/Doctrine/ORM/Query/SqlWalker.php | 10 +++++----- .../Tests/Models/CustomType/CustomTypeChild.php | 2 +- .../Tests/Models/CustomType/CustomTypeParent.php | 2 +- .../BasicEntityPersisterTypeValueSqlTest.php | 16 +++++++++++----- 8 files changed, 41 insertions(+), 20 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index b8c4ef2f1..c15b2d066 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -20,6 +20,7 @@ namespace Doctrine\ORM\Mapping; use Doctrine\Common\Persistence\Mapping\ClassMetadata; +use Doctrine\DBAL\Types\Type; use ReflectionClass; /** @@ -735,7 +736,7 @@ class ClassMetadataInfo implements ClassMetadata // Complete id mapping if (isset($mapping['id']) && $mapping['id'] === true) { if ($this->versionField == $mapping['fieldName']) { - throw MappingException::cannotVersionIdField($this->name, $mapping['fieldName']); + throw MappingException::cannotVersionIdField($this->name, $mapping['fieldName'], $mapping['type']); } if ( ! in_array($mapping['fieldName'], $this->identifier)) { @@ -746,6 +747,14 @@ class ClassMetadataInfo implements ClassMetadata $this->isIdentifierComposite = true; } } + + if (Type::hasType($mapping['type']) && Type::getType($mapping['type'])->canRequireSQLConversion()) { + if (isset($mapping['id']) && $mapping['id'] === true) { + throw MappingException::sqlConversionNotAllowedForIdentifiers($this->name, $mapping['fieldName']); + } + + $mapping['requireSQLConversion'] = true; + } } /** diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index e32e34c16..9c23d6f1a 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("Setting Id field '$fieldName' as versionale in entity class '$className' is not supported."); } + public static function sqlConversionNotAllowedForIdentifiers($className, $fieldName, $type) + { + return new self("It is not possible to set id field '$fieldName' to type '$type' in entity class '$className'. The type '$type' requires conversion SQL which is not allowed for identifiers."); + } + /** * @param string $className * @param string $columnName diff --git a/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php b/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php index 4f45f0877..e3bb9a943 100644 --- a/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php +++ b/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php @@ -65,10 +65,10 @@ abstract class AbstractEntityInheritancePersister extends BasicEntityPersister $columnAlias = $this->getSQLColumnAlias($columnName); $this->_rsm->addFieldResult($alias, $columnAlias, $field, $class->name); - if (!$class->isIdentifier($field)) { + if (isset($class->fieldMappings[$field]['requireSQLConversion'])) { $type = Type::getType($class->getTypeOfField($field)); $sql = $type->convertToPHPValueSQL($sql, $this->_platform); - } + } return $sql . ' AS ' . $columnAlias; } diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 41fb01a75..16dace847 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -344,7 +344,7 @@ class BasicEntityPersister if (isset($this->_class->fieldNames[$columnName])) { $column = $this->_class->getQuotedColumnName($this->_class->fieldNames[$columnName], $this->_platform); - if (!$this->_class->isIdentifier($this->_class->fieldNames[$columnName])) { + if (isset($this->_class->fieldMappings[$this->_class->fieldNames[$columnName]]['requireSQLConversion'])) { $type = Type::getType($this->_columnTypes[$columnName]); $placeholder = $type->convertToDatabaseValueSQL('?', $this->_platform); } @@ -1131,7 +1131,8 @@ class BasicEntityPersister foreach ($columns AS $column) { $placeholder = '?'; - if (isset($this->_columnTypes[$column])) { + if (isset($this->_columnTypes[$column]) && + isset($this->_class->fieldMappings[$this->_class->fieldNames[$column]]['requireSQLConversion'])) { $type = Type::getType($this->_columnTypes[$column]); $placeholder = $type->convertToDatabaseValueSQL('?', $this->_platform); } @@ -1198,8 +1199,8 @@ class BasicEntityPersister $columnAlias = $this->getSQLColumnAlias($class->columnNames[$field]); $this->_rsm->addFieldResult($alias, $columnAlias, $field); - - if (!$class->isIdentifier($field)) { + + if (isset($class->fieldMappings[$field]['requireSQLConversion'])) { $type = Type::getType($class->getTypeOfField($field)); $sql = $type->convertToPHPValueSQL($sql, $this->_platform); } @@ -1295,7 +1296,7 @@ class BasicEntityPersister $conditionSql .= $this->_getSQLTableAlias($className) . '.' . $this->_class->getQuotedColumnName($field, $this->_platform); - if (!$this->_class->isIdentifier($field)) { + if (isset($this->_class->fieldMappings[$field]['requireSQLConversion'])) { $type = Type::getType($this->_class->getTypeOfField($field)); $placeholder = $type->convertToDatabaseValueSQL($placeholder, $this->_platform); } diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 382796631..ccf6f2ca8 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -485,7 +485,7 @@ class SqlWalker implements TreeWalker $column .= $class->getQuotedColumnName($fieldName, $this->_platform); - if ($this->_useDbalTypeValueSql && !$class->isIdentifier($fieldName)) { + if ($this->_useDbalTypeValueSql && isset($class->fieldMappings[$fieldName]['requireSQLConversion'])) { $type = Type::getType($class->getTypeOfField($fieldName)); $column = $type->convertToPHPValueSQL($column, $this->_conn->getDatabasePlatform()); } @@ -1028,7 +1028,7 @@ class SqlWalker implements TreeWalker $col = $sqlTableAlias . '.' . $columnName; - if (!$class->isIdentifier($fieldName)) { + if (isset($class->fieldMappings[$fieldName]['requireSQLConversion'])) { $type = Type::getType($class->getTypeOfField($fieldName)); $col = $type->convertToPHPValueSQL($col, $this->_conn->getDatabasePlatform()); } @@ -1117,7 +1117,7 @@ class SqlWalker implements TreeWalker $col = $sqlTableAlias . '.' . $quotedColumnName; - if (!$class->isIdentifier($fieldName)) { + if (isset($class->fieldMappings[$fieldName]['requireSQLConversion'])) { $type = Type::getType($class->getTypeOfField($fieldName)); $col = $type->convertToPHPValueSQL($col, $this->_platform); } @@ -1146,7 +1146,7 @@ class SqlWalker implements TreeWalker $col = $sqlTableAlias . '.' . $quotedColumnName; - if (!$subClass->isIdentifier($fieldName)) { + if (isset($subClass->fieldMappings[$fieldName]['requireSQLConversion'])) { $type = Type::getType($subClass->getTypeOfField($fieldName)); $col = $type->convertToPHPValueSQL($col, $this->_platform); } @@ -1434,7 +1434,7 @@ class SqlWalker implements TreeWalker if ($updateItem->pathExpression->type == AST\PathExpression::TYPE_STATE_FIELD) { $class = $this->_queryComponents[$updateItem->pathExpression->identificationVariable]['metadata']; - if (!$class->isIdentifier($updateItem->pathExpression->field)) { + if (isset($class->fieldMappings[$updateItem->pathExpression->field]['requireSQLConversion'])) { $this->_currentColumnType = $class->getTypeOfField($updateItem->pathExpression->field); } } diff --git a/tests/Doctrine/Tests/Models/CustomType/CustomTypeChild.php b/tests/Doctrine/Tests/Models/CustomType/CustomTypeChild.php index 799ad5016..e178ab51c 100644 --- a/tests/Doctrine/Tests/Models/CustomType/CustomTypeChild.php +++ b/tests/Doctrine/Tests/Models/CustomType/CustomTypeChild.php @@ -9,7 +9,7 @@ namespace Doctrine\Tests\Models\CustomType; class CustomTypeChild { /** - * @Id @Column(type="negative_to_positive") + * @Id @Column(type="integer") * @GeneratedValue(strategy="AUTO") */ public $id; diff --git a/tests/Doctrine/Tests/Models/CustomType/CustomTypeParent.php b/tests/Doctrine/Tests/Models/CustomType/CustomTypeParent.php index 0ade61c91..1cc3126e4 100644 --- a/tests/Doctrine/Tests/Models/CustomType/CustomTypeParent.php +++ b/tests/Doctrine/Tests/Models/CustomType/CustomTypeParent.php @@ -9,7 +9,7 @@ namespace Doctrine\Tests\Models\CustomType; class CustomTypeParent { /** - * @Id @Column(type="negative_to_positive") + * @Id @Column(type="integer") * @GeneratedValue(strategy="AUTO") */ public $id; diff --git a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php index e608e6ba8..04c47893d 100644 --- a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php +++ b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php @@ -18,16 +18,22 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase protected function setUp() { parent::setUp(); - - $this->_em = $this->_getTestEntityManager(); - - $this->_persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata("Doctrine\Tests\Models\CustomType\CustomTypeParent")); - + if (DBALType::hasType('negative_to_positive')) { DBALType::overrideType('negative_to_positive', '\Doctrine\Tests\DbalTypes\NegativeToPositiveType'); } else { DBALType::addType('negative_to_positive', '\Doctrine\Tests\DbalTypes\NegativeToPositiveType'); } + + if (DBALType::hasType('upper_case_string')) { + DBALType::overrideType('upper_case_string', '\Doctrine\Tests\DbalTypes\UpperCaseStringType'); + } else { + DBALType::addType('upper_case_string', '\Doctrine\Tests\DbalTypes\UpperCaseStringType'); + } + + $this->_em = $this->_getTestEntityManager(); + + $this->_persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata("Doctrine\Tests\Models\CustomType\CustomTypeParent")); } public function testGetInsertSQLUsesTypeValuesSQL()