Skip to content

Commit

Permalink
Move check for conversion SQL to ClassMetadataInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Nov 20, 2011
1 parent 6f35679 commit 841d12e
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 20 deletions.
11 changes: 10 additions & 1 deletion lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace Doctrine\ORM\Mapping;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\DBAL\Types\Type;
use ReflectionClass;

/**
Expand Down Expand Up @@ -735,7 +736,7 @@ protected function _validateAndCompleteFieldMapping(array &$mapping)
// 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)) {
Expand All @@ -746,6 +747,14 @@ protected function _validateAndCompleteFieldMapping(array &$mapping)
$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;
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ORM/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ public static function cannotVersionIdField($className, $fieldName)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ protected function _getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r
$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;
}
Expand Down
11 changes: 6 additions & 5 deletions lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ protected final function _updateTable($entity, $quotedTableName, array $updateDa
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);
}
Expand Down Expand Up @@ -1131,7 +1131,8 @@ protected function _getInsertSQL()
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);
}
Expand Down Expand Up @@ -1198,8 +1199,8 @@ protected function _getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r
$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);
}
Expand Down Expand Up @@ -1295,7 +1296,7 @@ protected function _getSelectConditionSQL(array $criteria, $assoc = null)

$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);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public function walkPathExpression($pathExpr)

$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());
}
Expand Down Expand Up @@ -1028,7 +1028,7 @@ public function walkSelectExpression($selectExpression)

$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());
}
Expand Down Expand Up @@ -1117,7 +1117,7 @@ public function walkSelectExpression($selectExpression)

$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);
}
Expand Down Expand Up @@ -1146,7 +1146,7 @@ public function walkSelectExpression($selectExpression)

$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);
}
Expand Down Expand Up @@ -1434,7 +1434,7 @@ public function walkUpdateItem($updateItem)

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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Models/CustomType/CustomTypeChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class CustomTypeChild
{
/**
* @Id @Column(type="negative_to_positive")
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class CustomTypeParent
{
/**
* @Id @Column(type="negative_to_positive")
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 841d12e

Please sign in to comment.