Skip to content

Commit

Permalink
Make Query::insert() behave when called multiple times.
Browse files Browse the repository at this point in the history
When insert() is invoked multiple times, it should reset the
ValueExpression. Not doing so causes an incorrect query to be generated.

Refs #8815
  • Loading branch information
markstory committed May 12, 2016
1 parent f61575f commit dc5cc8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Database/Query.php
Expand Up @@ -1320,10 +1320,7 @@ public function insert(array $columns, array $types = [])
$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;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -2719,6 +2719,31 @@ public function testInsertNothing()
$query->insert([]);
}

/**
* 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.
*
Expand Down

0 comments on commit dc5cc8b

Please sign in to comment.