Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

Merged
merged 1 commit into from 11 months ago

2 participants

Fabio B. Silva Benjamin Eberlei
Benjamin Eberlei beberlei commented on the diff
lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php
@@ -127,12 +127,15 @@ function ($joinColumn) use ($platform)
127 127
      */
128 128
     public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null)
2
Benjamin Eberlei Owner
beberlei added a note

Wondering if we should ship an AnsiQuoteStrategy that just assumes every column is ANSI compliant and only does return $platform->getSQLResultCashing($columnName . $counter);

Fabio B. Silva Owner

Sounds good.

I've created an issue : DDC-2459

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Benjamin Eberlei beberlei merged commit c9d9b68 into from
Benjamin Eberlei beberlei closed this
Fabio B. Silva FabioBatSilva deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
9  lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php
@@ -127,12 +127,15 @@ function ($joinColumn) use ($platform)
127 127
      */
128 128
     public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null)
129 129
     {
130  
-        // Trim the column alias to the maximum identifier length of the platform.
131  
-        // If the alias is to long, characters are cut off from the beginning.
132  
-        // And strip non alphanumeric characters
  130
+        // 1 ) Concatenate column name and counter
  131
+        // 2 ) Trim the column alias to the maximum identifier length of the platform.
  132
+        //     If the alias is to long, characters are cut off from the beginning.
  133
+        // 3 ) Strip non alphanumeric characters
  134
+        // 4 ) Prefix with "_" if the result its numeric
133 135
         $columnName = $columnName . $counter;
134 136
         $columnName = substr($columnName, -$platform->getMaxIdentifierLength());
135 137
         $columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
  138
+        $columnName = is_numeric($columnName) ? '_' . $columnName : $columnName;
136 139
 
137 140
         return $platform->getSQLResultCasing($columnName);
138 141
     }
31  tests/Doctrine/Tests/Models/Quote/NumericEntity.php
... ...
@@ -0,0 +1,31 @@
  1
+<?php
  2
+
  3
+namespace Doctrine\Tests\Models\Quote;
  4
+
  5
+/**
  6
+ * @Entity
  7
+ * @Table(name="table")
  8
+ */
  9
+class NumericEntity
  10
+{
  11
+    /**
  12
+     * @Id
  13
+     * @Column(type="integer", name="`1:1`")
  14
+     * @GeneratedValue(strategy="AUTO")
  15
+     */
  16
+    public $id;
  17
+
  18
+    /**
  19
+     * @Column(type="string", name="`2:2`")
  20
+     */
  21
+    public $value;
  22
+
  23
+    /**
  24
+     * @param string $value
  25
+     */
  26
+    public function __construct($value)
  27
+    {
  28
+        $this->value = $value;
  29
+    }
  30
+
  31
+}
21  tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
@@ -1810,6 +1810,27 @@ public function testStripNonAlphanumericCharactersFromAlias()
1810 1810
         );
1811 1811
     }
1812 1812
 
  1813
+    /**
  1814
+     * @group DDC-2435
  1815
+     */
  1816
+    public function testColumnNameWithNumbersAndNonAlphanumericCharacters()
  1817
+    {
  1818
+        $this->assertSqlGeneration(
  1819
+            'SELECT e FROM Doctrine\Tests\Models\Quote\NumericEntity e',
  1820
+            'SELECT t0_."1:1" AS _110, t0_."2:2" AS _221 FROM table t0_'
  1821
+        );
  1822
+
  1823
+        $this->assertSqlGeneration(
  1824
+            'SELECT e.value FROM Doctrine\Tests\Models\Quote\NumericEntity e',
  1825
+            'SELECT t0_."2:2" AS _220 FROM table t0_'
  1826
+        );
  1827
+
  1828
+        $this->assertSqlGeneration(
  1829
+            'SELECT TRIM(e.value) FROM Doctrine\Tests\Models\Quote\NumericEntity e',
  1830
+            'SELECT TRIM(t0_."2:2") AS sclr0 FROM table t0_'
  1831
+        );
  1832
+    }
  1833
+
1813 1834
    /**
1814 1835
     * @group DDC-1845
1815 1836
     */
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.