From cafb4adfef041007a8c24c26d0411b18b074aa69 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 12 May 2016 22:44:07 -0400 Subject: [PATCH] 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. --- src/Database/Expression/ValuesExpression.php | 9 +++++++-- src/Database/Query.php | 7 +++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Database/Expression/ValuesExpression.php b/src/Database/Expression/ValuesExpression.php index 512e3a81f35..e57804868af 100644 --- a/src/Database/Expression/ValuesExpression.php +++ b/src/Database/Expression/ValuesExpression.php @@ -151,7 +151,13 @@ public function sql(ValueBinder $generator) } $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 @@ public function sql(ValueBinder $generator) if ($this->query()) { return ' ' . $this->query()->sql($generator); } - return sprintf(' VALUES (%s)', implode('), (', $placeholders)); } diff --git a/src/Database/Query.php b/src/Database/Query.php index 05b0f0ce6bc..fde3cc4da9c 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -1320,8 +1320,11 @@ public function insert(array $columns, array $types = []) $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; }