1
0
mirror of synced 2025-01-18 06:21:40 +03:00

Move check for conversion SQL to ClassMetadataInfo

This commit is contained in:
Jan Sorgalla 2011-11-20 19:50:51 +01:00
parent 6f35679911
commit 841d12e9b6
8 changed files with 41 additions and 20 deletions

View File

@ -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;
}
}
/**

View File

@ -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

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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()