1
0
mirror of synced 2025-01-18 22:41:43 +03:00

[DDC-2435] Fix column name with numbers and non alphanumeric characters.

This commit is contained in:
Fabio B. Silva 2013-05-17 12:44:48 -03:00
parent 65886fdfea
commit f92214997f
3 changed files with 58 additions and 3 deletions

View File

@ -127,12 +127,15 @@ class DefaultQuoteStrategy implements QuoteStrategy
*/
public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null)
{
// Trim the column alias to the maximum identifier length of the platform.
// If the alias is to long, characters are cut off from the beginning.
// And strip non alphanumeric characters
// 1 ) Concatenate column name and counter
// 2 ) Trim the column alias to the maximum identifier length of the platform.
// If the alias is to long, characters are cut off from the beginning.
// 3 ) Strip non alphanumeric characters
// 4 ) Prefix with "_" if the result its numeric
$columnName = $columnName . $counter;
$columnName = substr($columnName, -$platform->getMaxIdentifierLength());
$columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
$columnName = is_numeric($columnName) ? '_' . $columnName : $columnName;
return $platform->getSQLResultCasing($columnName);
}

View File

@ -0,0 +1,31 @@
<?php
namespace Doctrine\Tests\Models\Quote;
/**
* @Entity
* @Table(name="table")
*/
class NumericEntity
{
/**
* @Id
* @Column(type="integer", name="`1:1`")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @Column(type="string", name="`2:2`")
*/
public $value;
/**
* @param string $value
*/
public function __construct($value)
{
$this->value = $value;
}
}

View File

@ -1810,6 +1810,27 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
);
}
/**
* @group DDC-2435
*/
public function testColumnNameWithNumbersAndNonAlphanumericCharacters()
{
$this->assertSqlGeneration(
'SELECT e FROM Doctrine\Tests\Models\Quote\NumericEntity e',
'SELECT t0_."1:1" AS _110, t0_."2:2" AS _221 FROM table t0_'
);
$this->assertSqlGeneration(
'SELECT e.value FROM Doctrine\Tests\Models\Quote\NumericEntity e',
'SELECT t0_."2:2" AS _220 FROM table t0_'
);
$this->assertSqlGeneration(
'SELECT TRIM(e.value) FROM Doctrine\Tests\Models\Quote\NumericEntity e',
'SELECT TRIM(t0_."2:2") AS sclr0 FROM table t0_'
);
}
/**
* @group DDC-1845
*/