From 9219a30d80ca10debb2fd4d48b3fe82428733bea Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 14 Sep 2016 05:21:07 -0400 Subject: [PATCH 1/5] add a $default parameter to the QB when() clause allows you to specify a default in the case that the when() $value is false --- src/Illuminate/Database/Query/Builder.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 3fb73f8e6d36..2fe3f0ba6b6b 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -459,14 +459,17 @@ public function crossJoin($table, $first = null, $operator = null, $second = nul * * @param bool $value * @param \Closure $callback + * @param \Closure $default * @return \Illuminate\Database\Query\Builder */ - public function when($value, $callback) + public function when($value, $callback, $default = null) { $builder = $this; if ($value) { $builder = call_user_func($callback, $builder); + } elseif ($default) { + $builder = call_user_func($default, $builder); } return $builder; From 6dd3d44db59eb9bc2ba1a7c7e9c609f90e199b2f Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 14 Sep 2016 05:25:17 -0400 Subject: [PATCH 2/5] add new test for when() method to test $default --- tests/Database/DatabaseQueryBuilderTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index ab6783b7b197..eec3de7385be 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -134,6 +134,27 @@ public function testWhenCallback() $builder->select('*')->from('users')->when(false, $callback)->where('email', 'foo'); $this->assertEquals('select * from "users" where "email" = ?', $builder->toSql()); } + + public function testWhenCallbackWithDefault() + { + $callback = function ($query) { + return $query->where('id', '=', 1); + }; + + $default = function ($query) { + return $query->where('id', '=', 2); + }; + + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->when(true, $callback, $default)->where('email', 'foo'); + $this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql()); + $this->assertEquals([0 => 1], $builder->getBindings()); + + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->when(false, $callback, $default)->where('email', 'foo'); + $this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql()); + $this->assertEquals([0 => 2], $builder->getBindings()); + } public function testBasicWheres() { From 6f715ee2f45870a825cca2cb1e26d42296a26a24 Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 14 Sep 2016 05:33:38 -0400 Subject: [PATCH 3/5] remove extra spaces to make StyleCI happy --- tests/Database/DatabaseQueryBuilderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index eec3de7385be..308215ed8450 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -134,13 +134,13 @@ public function testWhenCallback() $builder->select('*')->from('users')->when(false, $callback)->where('email', 'foo'); $this->assertEquals('select * from "users" where "email" = ?', $builder->toSql()); } - + public function testWhenCallbackWithDefault() { $callback = function ($query) { return $query->where('id', '=', 1); }; - + $default = function ($query) { return $query->where('id', '=', 2); }; From a8ea7a381ad152b36929623f95345bc1020843bd Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 14 Sep 2016 05:35:34 -0400 Subject: [PATCH 4/5] use the correct index for the bindings in the test --- tests/Database/DatabaseQueryBuilderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 308215ed8450..5f3b8c932131 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -148,12 +148,12 @@ public function testWhenCallbackWithDefault() $builder = $this->getBuilder(); $builder->select('*')->from('users')->when(true, $callback, $default)->where('email', 'foo'); $this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql()); - $this->assertEquals([0 => 1], $builder->getBindings()); + $this->assertEquals([1 => 1], $builder->getBindings()); $builder = $this->getBuilder(); $builder->select('*')->from('users')->when(false, $callback, $default)->where('email', 'foo'); $this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql()); - $this->assertEquals([0 => 2], $builder->getBindings()); + $this->assertEquals([1 => 2], $builder->getBindings()); } public function testBasicWheres() From 6c0a740dab1b0b0d6d56ada939ca6af4bcf27041 Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 14 Sep 2016 05:36:42 -0400 Subject: [PATCH 5/5] use the full bindings array --- tests/Database/DatabaseQueryBuilderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 5f3b8c932131..3f36254cce8f 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -148,12 +148,12 @@ public function testWhenCallbackWithDefault() $builder = $this->getBuilder(); $builder->select('*')->from('users')->when(true, $callback, $default)->where('email', 'foo'); $this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql()); - $this->assertEquals([1 => 1], $builder->getBindings()); + $this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings()); $builder = $this->getBuilder(); $builder->select('*')->from('users')->when(false, $callback, $default)->where('email', 'foo'); $this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql()); - $this->assertEquals([1 => 2], $builder->getBindings()); + $this->assertEquals([0 => 2, 1 => 'foo'], $builder->getBindings()); } public function testBasicWheres()