Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52a0b58

Browse files
committed
Fix queries with whereIn and empty arrays
1 parent 624617d commit 52a0b58

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/Illuminate/Database/Query/Grammars/Grammar.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ protected function whereNotExists(Builder $query, $where)
306306
*/
307307
protected function whereIn(Builder $query, $where)
308308
{
309+
if (empty($where['values'])) return '0';
310+
309311
$values = $this->parameterize($where['values']);
310312

311313
return $this->wrap($where['column']).' in ('.$values.')';
@@ -320,6 +322,8 @@ protected function whereIn(Builder $query, $where)
320322
*/
321323
protected function whereNotIn(Builder $query, $where)
322324
{
325+
if (empty($where['values'])) return '1';
326+
323327
$values = $this->parameterize($where['values']);
324328

325329
return $this->wrap($where['column']).' not in ('.$values.')';
@@ -376,7 +380,7 @@ protected function whereNotNull(Builder $query, $where)
376380
{
377381
return $this->wrap($where['column']).' is not null';
378382
}
379-
383+
380384
/**
381385
* Compile a "where date" clause.
382386
*
@@ -388,7 +392,7 @@ protected function whereDate(Builder $query, $where)
388392
{
389393
return $this->dateBasedWhere('date', $query, $where);
390394
}
391-
395+
392396
/**
393397
* Compile a "where day" clause.
394398
*

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,34 @@ public function testBasicWhereNotIns()
343343
}
344344

345345

346+
public function testEmptyWhereIns()
347+
{
348+
$builder = $this->getBuilder();
349+
$builder->select('*')->from('users')->whereIn('id', array());
350+
$this->assertEquals('select * from "users" where 0', $builder->toSql());
351+
$this->assertEquals(array(), $builder->getBindings());
352+
353+
$builder = $this->getBuilder();
354+
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereIn('id', array());
355+
$this->assertEquals('select * from "users" where "id" = ? or 0', $builder->toSql());
356+
$this->assertEquals(array(0 => 1), $builder->getBindings());
357+
}
358+
359+
360+
public function testEmptyWhereNotIns()
361+
{
362+
$builder = $this->getBuilder();
363+
$builder->select('*')->from('users')->whereNotIn('id', array());
364+
$this->assertEquals('select * from "users" where 1', $builder->toSql());
365+
$this->assertEquals(array(), $builder->getBindings());
366+
367+
$builder = $this->getBuilder();
368+
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereNotIn('id', array());
369+
$this->assertEquals('select * from "users" where "id" = ? or 1', $builder->toSql());
370+
$this->assertEquals(array(0 => 1), $builder->getBindings());
371+
}
372+
373+
346374
public function testUnions()
347375
{
348376
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)