From 82b6e32840e79e1c4bd43520991564872bf8d8f9 Mon Sep 17 00:00:00 2001 From: Sebastien Armand Date: Wed, 22 Jan 2020 16:03:38 -0800 Subject: [PATCH 1/4] more depth to the whereins --- src/Database/EloquentBuilder.php | 6 ++++++ tests/Integration/MutatorTest.php | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Database/EloquentBuilder.php b/src/Database/EloquentBuilder.php index 7e22934..12dcd22 100644 --- a/src/Database/EloquentBuilder.php +++ b/src/Database/EloquentBuilder.php @@ -93,11 +93,17 @@ public function whereIn($column, $values, $boolean = 'and', $not = false) return $this; } + if ($where['type'] === 'InSub') { + $this->query->wheres[] = $where; + return parent::whereIn($column, $values, $boolean, $not); + } + // Get the column name $mutatedColumn = $this->getUnqualifiedColumnName($where['column']); // Loop over all values and mutate them $mutatedValues = []; + $values = $where['values'] ?? null; foreach ($where['values'] as $value) { if ($value instanceof Expression) { $mutatedValues[] = $value; diff --git a/tests/Integration/MutatorTest.php b/tests/Integration/MutatorTest.php index c1d5eef..3742775 100644 --- a/tests/Integration/MutatorTest.php +++ b/tests/Integration/MutatorTest.php @@ -125,13 +125,25 @@ public function test_where_in_subquery() (new TestModel())->create(['id' => $id, 'name' => 'A chair']); (new TestModel())->create(['id' => $id2, 'name' => 'A table']); $p = TestModel::whereIn('id', function ($query) { - $query->select('id')->from('test_model'); + $query->select('id')->from('test_model')->whereNull('location'); })->get(); $this->assertEquals(2, $p->count()); $this->assertEquals($id, $p->first()->id); $this->assertEquals($id2, $p->last()->id); } + public function test_where_in_wherein_subquery() + { + $id = Uuid::uuid1()->toString(); + $id2 = Uuid::uuid1()->toString(); + (new TestModel())->create(['id' => $id, 'name' => 'A chair']); + (new TestModel())->create(['id' => $id2, 'name' => 'A table']); + $p = TestModel::withWherein()->get(); + $this->assertEquals(2, $p->count()); + $this->assertEquals($id, $p->first()->id); + $this->assertEquals($id2, $p->last()->id); + } + public function test_where_not_in() { $id = Uuid::uuid1()->toString(); @@ -256,6 +268,14 @@ class TestModel extends Model 'id' => 'uuid_v1_binary', 'location' => 'encrypt_string', ]; + + public function scopeWithWherein($builder) + { + $builder->whereIn('id', function ($subquery) { + $subquery->select('id')->from('test_model') + ->whereNull('location'); + }); + } } class TimestampedModel extends Model From 4449a9cdcac66e802ae435a35ddb9ec89b6ec4b7 Mon Sep 17 00:00:00 2001 From: Elliot Fehr Date: Thu, 23 Jan 2020 00:03:53 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- src/Database/EloquentBuilder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Database/EloquentBuilder.php b/src/Database/EloquentBuilder.php index 12dcd22..840133c 100644 --- a/src/Database/EloquentBuilder.php +++ b/src/Database/EloquentBuilder.php @@ -95,6 +95,7 @@ public function whereIn($column, $values, $boolean = 'and', $not = false) if ($where['type'] === 'InSub') { $this->query->wheres[] = $where; + return parent::whereIn($column, $values, $boolean, $not); } From c0476c47d2eef5d2886cc1f2e922614ac13262e8 Mon Sep 17 00:00:00 2001 From: Sebastien Armand Date: Thu, 23 Jan 2020 11:24:57 -0800 Subject: [PATCH 3/4] Update src/Database/EloquentBuilder.php Co-Authored-By: Bez Hermoso --- src/Database/EloquentBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/EloquentBuilder.php b/src/Database/EloquentBuilder.php index 840133c..b02be9d 100644 --- a/src/Database/EloquentBuilder.php +++ b/src/Database/EloquentBuilder.php @@ -93,7 +93,7 @@ public function whereIn($column, $values, $boolean = 'and', $not = false) return $this; } - if ($where['type'] === 'InSub') { + if ($where['type'] === 'InSub' || $where['type'] === 'NotInSub') { $this->query->wheres[] = $where; return parent::whereIn($column, $values, $boolean, $not); From 4686862681881412b373b1402f5a67611be9901f Mon Sep 17 00:00:00 2001 From: Sebastien Armand Date: Thu, 23 Jan 2020 11:25:42 -0800 Subject: [PATCH 4/4] Update EloquentBuilder.php --- src/Database/EloquentBuilder.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Database/EloquentBuilder.php b/src/Database/EloquentBuilder.php index b02be9d..7ccbbcd 100644 --- a/src/Database/EloquentBuilder.php +++ b/src/Database/EloquentBuilder.php @@ -104,7 +104,6 @@ public function whereIn($column, $values, $boolean = 'and', $not = false) // Loop over all values and mutate them $mutatedValues = []; - $values = $where['values'] ?? null; foreach ($where['values'] as $value) { if ($value instanceof Expression) { $mutatedValues[] = $value;