Skip to content

Commit

Permalink
Merge pull request #29272 from staudenmeir/update-bindings
Browse files Browse the repository at this point in the history
[5.8] Fix UPDATE query bindings on PostgreSQL
  • Loading branch information
taylorotwell authored Jul 23, 2019
2 parents b41c04a + 0708161 commit 73da739
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ public function compileUpdate(Builder $query, $values)
*/
public function prepareBindingsForUpdate(array $bindings, array $values)
{
$cleanBindings = Arr::except($bindings, ['join', 'select']);
$cleanBindings = Arr::except($bindings, ['select', 'join']);

return array_values(
array_merge($bindings['join'], $values, Arr::flatten($cleanBindings))
Expand Down
7 changes: 2 additions & 5 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,10 @@ public function prepareBindingsForUpdate(array $bindings, array $values)
: $value;
})->all();

// Update statements with "joins" in Postgres use an interesting syntax. We need to
// take all of the bindings and put them on the end of this array since they are
// added to the end of the "where" clause statements as typical where clauses.
$bindingsWithoutJoin = Arr::except($bindings, 'join');
$cleanBindings = Arr::except($bindings, 'select');

return array_values(
array_merge($values, $bindings['join'], Arr::flatten($bindingsWithoutJoin))
array_merge($values, Arr::flatten($cleanBindings))
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ public function compileUpdate(Builder $query, $values)
*/
public function prepareBindingsForUpdate(array $bindings, array $values)
{
$cleanBindings = Arr::except($bindings, ['select', 'join']);
$cleanBindings = Arr::except($bindings, 'select');

return array_values(
array_merge($values, $bindings['join'], Arr::flatten($cleanBindings))
array_merge($values, Arr::flatten($cleanBindings))
);
}

Expand Down
7 changes: 2 additions & 5 deletions src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,10 @@ protected function parseUpdateTable($table)
*/
public function prepareBindingsForUpdate(array $bindings, array $values)
{
// Update statements with joins in SQL Servers utilize an unique syntax. We need to
// take all of the bindings and put them on the end of this array since they are
// added to the end of the "where" clause statements as typical where clauses.
$bindingsWithoutJoin = Arr::except($bindings, 'join');
$cleanBindings = Arr::except($bindings, 'select');

return array_values(
array_merge($values, $bindings['join'], Arr::flatten($bindingsWithoutJoin))
array_merge($values, Arr::flatten($cleanBindings))
);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,11 @@ public function testUpdateMethodWithoutJoinsOnPostgres()
$builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "id" = ?', ['foo', 'bar', 1])->andReturn(1);
$result = $builder->from('users')->where('id', '=', 1)->update(['users.email' => 'foo', 'name' => 'bar']);
$this->assertEquals(1, $result);

$builder = $this->getPostgresBuilder();
$builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "id" = ?', ['foo', 'bar', 1])->andReturn(1);
$result = $builder->from('users')->where('id', '=', 1)->selectRaw('?', ['ignore'])->update(['users.email' => 'foo', 'name' => 'bar']);
$this->assertEquals(1, $result);
}

public function testUpdateMethodWithJoinsOnPostgres()
Expand Down

0 comments on commit 73da739

Please sign in to comment.