Skip to content

Commit dc5cc8b

Browse files
committed
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
1 parent f61575f commit dc5cc8b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Database/Query.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,10 +1320,7 @@ public function insert(array $columns, array $types = [])
13201320
$this->_dirty();
13211321
$this->_type = 'insert';
13221322
$this->_parts['insert'][1] = $columns;
1323-
1324-
if (!$this->_parts['values']) {
1325-
$this->_parts['values'] = new ValuesExpression($columns, $this->typeMap()->types($types));
1326-
}
1323+
$this->_parts['values'] = new ValuesExpression($columns, $this->typeMap()->types($types));
13271324

13281325
return $this;
13291326
}

tests/TestCase/Database/QueryTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,6 +2719,31 @@ public function testInsertNothing()
27192719
$query->insert([]);
27202720
}
27212721

2722+
/**
2723+
* Test insert overwrites values
2724+
*
2725+
* @return void
2726+
*/
2727+
public function testInsertOverwritesValues()
2728+
{
2729+
$this->loadFixtures('Articles');
2730+
$query = new Query($this->connection);
2731+
$query->insert(['title', 'body'])
2732+
->insert(['title'])
2733+
->into('articles')
2734+
->values([
2735+
'title' => 'mark',
2736+
]);
2737+
2738+
$result = $query->sql();
2739+
$this->assertQuotedQuery(
2740+
'INSERT INTO <articles> \(<title>\) (OUTPUT INSERTED\.\* )?' .
2741+
'VALUES \(:c0\)',
2742+
$result,
2743+
!$this->autoQuote
2744+
);
2745+
}
2746+
27222747
/**
27232748
* Test inserting a single row.
27242749
*

0 commit comments

Comments
 (0)