Browse Source

Fix insertion errors from the previous change.

Strip out common identifier quoting characters so that the default row
has the same keys as the data being inserted.
Mark Story 10 years ago
parent
commit
cafb4adfef
2 changed files with 12 additions and 4 deletions
  1. 7 2
      src/Database/Expression/ValuesExpression.php
  2. 5 2
      src/Database/Query.php

+ 7 - 2
src/Database/Expression/ValuesExpression.php

@@ -151,7 +151,13 @@ class ValuesExpression implements ExpressionInterface
         }
 
         $i = 0;
-        $defaults = array_fill_keys($this->_columns, null);
+        $columns = [];
+
+        // Remove identifier quoting so column names match keys.
+        foreach ($this->_columns as $col) {
+            $columns[] = trim($col, '`[]"');
+        }
+        $defaults = array_fill_keys($columns, null);
         $placeholders = [];
 
         foreach ($this->_values as $row) {
@@ -173,7 +179,6 @@ class ValuesExpression implements ExpressionInterface
         if ($this->query()) {
             return ' ' . $this->query()->sql($generator);
         }
-
         return sprintf(' VALUES (%s)', implode('), (', $placeholders));
     }
 

+ 5 - 2
src/Database/Query.php

@@ -1320,8 +1320,11 @@ class Query implements ExpressionInterface, IteratorAggregate
         $this->_dirty();
         $this->_type = 'insert';
         $this->_parts['insert'][1] = $columns;
-        $this->_parts['values'] = new ValuesExpression($columns, $this->typeMap()->types($types));
-
+        if (!$this->_parts['values']) {
+            $this->_parts['values'] = new ValuesExpression($columns, $this->typeMap()->types($types));
+        } else {
+            $this->_parts['values']->columns($columns);
+        }
         return $this;
     }