Browse Source

Make Query::insert() behave when called multiple times.

When insert() is invoked multiple times, it should reset the
ValueExpression. Not doing so causes an incorrect query to be generated.

Refs #8815
Mark Story 10 years ago
parent
commit
dc5cc8b8b6
2 changed files with 26 additions and 4 deletions
  1. 1 4
      src/Database/Query.php
  2. 25 0
      tests/TestCase/Database/QueryTest.php

+ 1 - 4
src/Database/Query.php

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

+ 25 - 0
tests/TestCase/Database/QueryTest.php

@@ -2720,6 +2720,31 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Test insert overwrites values
+     *
+     * @return void
+     */
+    public function testInsertOverwritesValues()
+    {
+        $this->loadFixtures('Articles');
+        $query = new Query($this->connection);
+        $query->insert(['title', 'body'])
+            ->insert(['title'])
+            ->into('articles')
+            ->values([
+                'title' => 'mark',
+            ]);
+
+        $result = $query->sql();
+        $this->assertQuotedQuery(
+            'INSERT INTO <articles> \(<title>\) (OUTPUT INSERTED\.\* )?' .
+            'VALUES \(:c0\)',
+            $result,
+            !$this->autoQuote
+        );
+    }
+
+    /**
      * Test inserting a single row.
      *
      * @return void