Skip to content

Commit

Permalink
fix SqlServer compilation for delete with JOINs and using table aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored and taylorotwell committed Mar 1, 2017
1 parent d2b6c8b commit b93acdd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,13 @@ protected function compileDeleteWithJoins(Builder $query, $table, $where)
{
$joins = ' '.$this->compileJoins($query, $query->joins);

return trim("delete {$table} from {$table}{$joins} {$where}");
$alias = $table;

if (strpos(strtolower($table), ' as ') !== false) {
$alias = explode(' as ', $table)[1];
}

return trim("delete {$alias} from {$table}{$joins} {$where}");
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,11 @@ public function testDeleteWithJoinMethod()
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->where('email', '=', 'foo')->delete();
$this->assertEquals(1, $result);

$builder = $this->getSqlServerBuilder();
$builder->getConnection()->shouldReceive('delete')->once()->with('delete [a] from [users] as [a] inner join [users] as [b] on [a].[id] = [b].[user_id] where [email] = ?', ['foo'])->andReturn(1);
$result = $builder->from('users AS a')->join('users AS b', 'a.id', '=', 'b.user_id')->where('email', '=', 'foo')->orderBy('id')->limit(1)->delete();
$this->assertEquals(1, $result);

$builder = $this->getSqlServerBuilder();
$builder->getConnection()->shouldReceive('delete')->once()->with('delete [users] from [users] inner join [contacts] on [users].[id] = [contacts].[id] where [users].[id] = ?', [1])->andReturn(1);
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->delete(1);
Expand Down

0 comments on commit b93acdd

Please sign in to comment.