Permalink
Browse files

Merge pull request #671 from FabioBatSilva/DDC-2435

[DDC-2435] Fix column name with numbers and non alphanumeric characters.
  • Loading branch information...
2 parents 65886fd + f922149 commit c9d9b68fa9937218aad05dfca4b3f96b409cfc8e @beberlei beberlei committed May 17, 2013
@@ -127,12 +127,15 @@ function ($joinColumn) use ($platform)
*/
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);
}
@@ -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;
+ }
+
+}
@@ -1810,6 +1810,27 @@ public function testStripNonAlphanumericCharactersFromAlias()
);
}
+ /**
+ * @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
*/

0 comments on commit c9d9b68

Please sign in to comment.