From a18830fda88c5e4c1732b6c07c08414b5472773f Mon Sep 17 00:00:00 2001 From: Filip Horvat Date: Fri, 8 Feb 2019 10:18:59 +0100 Subject: [PATCH] Where in where not in (#49) --- README.md | 22 +++++++++++++-- src/EloquentJoinBuilder.php | 34 ++++++++++++++++++++++++ tests/Tests/Clauses/OrWhereInTest.php | 28 +++++++++++++++++++ tests/Tests/Clauses/OrWhereNotInTest.php | 27 +++++++++++++++++++ tests/Tests/Clauses/WhereInTest.php | 25 +++++++++++++++++ tests/Tests/Clauses/WhereNotInTest.php | 25 +++++++++++++++++ 6 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 tests/Tests/Clauses/OrWhereInTest.php create mode 100644 tests/Tests/Clauses/OrWhereNotInTest.php create mode 100644 tests/Tests/Clauses/WhereInTest.php create mode 100644 tests/Tests/Clauses/WhereNotInTest.php diff --git a/README.md b/README.md index 9cc1595..b914e23 100644 --- a/README.md +++ b/README.md @@ -229,11 +229,29 @@ Options are : **SUM**, **AVG**, **MAX**, **MIN**, **COUNT** **whereJoin($column, $operator, $value, $boolean = 'and')** -* ***$column***, ***$operator***, ***$value*** and ***$boolean*** arguments are the same as in default eloquent **where()** +* arguments are the same as in default eloquent **where()** **orWhereJoin($column, $operator, $value)** -* ***$column***, ***$operator*** and ***$value*** arguments are the same as in default eloquent **orWhere()** +* arguments are the same as in default eloquent **orWhere()** + + +**whereInJoin($column, $values, $boolean = 'and', $not = false)** + +* arguments are the same as in default eloquent **whereIn()** + +**whereNotInJoin($column, $values, $boolean = 'and')** + +* arguments are the same as in default eloquent **whereNotIn()** + +**orWhereInJoin($column, $values)** + +* arguments are the same as in default eloquent **orWhereIn()** + +**orWhereNotInJoin($column, $values, $boolean = 'and')** + +* arguments are the same as in default eloquent **orWhereNotIn()** + ### Allowed clauses on BelongsTo, HasOne and HasMany relations on which you can use join clauses on the query diff --git a/src/EloquentJoinBuilder.php b/src/EloquentJoinBuilder.php index 288bd2f..a9bf210 100644 --- a/src/EloquentJoinBuilder.php +++ b/src/EloquentJoinBuilder.php @@ -48,6 +48,7 @@ class EloquentJoinBuilder extends Builder //store clauses on relation for join public $relationClauses = []; + //query methods public function where($column, $operator = null, $value = null, $boolean = 'and') { if ($column instanceof \Closure) { @@ -81,6 +82,38 @@ public function orWhereJoin($column, $operator, $value) return $this->orWhere($column, $operator, $value); } + public function whereInJoin($column, $values, $boolean = 'and', $not = false) + { + $query = $this->baseBuilder ? $this->baseBuilder : $this; + $column = $query->performJoin($column); + + return $this->whereIn($column, $values, $boolean, $not); + } + + public function whereNotInJoin($column, $values, $boolean = 'and') + { + $query = $this->baseBuilder ? $this->baseBuilder : $this; + $column = $query->performJoin($column); + + return $this->whereNotIn($column, $values, $boolean); + } + + public function orWhereInJoin($column, $values) + { + $query = $this->baseBuilder ? $this->baseBuilder : $this; + $column = $query->performJoin($column); + + return $this->whereIn($column, $values, 'or'); + } + + public function orWhereNotInJoin($column, $values, $boolean = 'and') + { + $query = $this->baseBuilder ? $this->baseBuilder : $this; + $column = $query->performJoin($column); + + return $this->whereIn($column, $values, $boolean, true); + } + public function orderByJoin($column, $direction = 'asc', $aggregateMethod = null) { $dotPos = strrpos($column, '.'); @@ -115,6 +148,7 @@ public function joinRelations($relations, $leftJoin = null) return $this; } + //helpers methods private function performJoin($relations, $leftJoin = null) { //detect join method diff --git a/tests/Tests/Clauses/OrWhereInTest.php b/tests/Tests/Clauses/OrWhereInTest.php new file mode 100644 index 0000000..1e9ce5a --- /dev/null +++ b/tests/Tests/Clauses/OrWhereInTest.php @@ -0,0 +1,28 @@ +whereInJoin('seller.id', [1, 2]) + ->orWhereInJoin('seller.id', [3, 4]) + ->get(); + + $queryTest = 'select orders.* + from "orders" + left join "sellers" on "sellers"."id" = "orders"."seller_id" + where ("sellers"."id" in (?, ?) + or + "sellers"."id" in (?, ?)) + and "orders"."deleted_at" is null + group by "orders"."id"'; + + $this->assertQueryMatches($queryTest, $this->fetchQuery()); + } +} diff --git a/tests/Tests/Clauses/OrWhereNotInTest.php b/tests/Tests/Clauses/OrWhereNotInTest.php new file mode 100644 index 0000000..4c829db --- /dev/null +++ b/tests/Tests/Clauses/OrWhereNotInTest.php @@ -0,0 +1,27 @@ +whereInJoin('seller.id', [1, 2]) + ->orWhereNotInJoin('seller.id', [3, 4]) + ->get(); + + $queryTest = 'select orders.* + from "orders" + left join "sellers" on "sellers"."id" = "orders"."seller_id" + where "sellers"."id" in (?, ?) + and "sellers"."id" not in (?, ?) + and "orders"."deleted_at" is null + group by "orders"."id"'; + + $this->assertQueryMatches($queryTest, $this->fetchQuery()); + } +} diff --git a/tests/Tests/Clauses/WhereInTest.php b/tests/Tests/Clauses/WhereInTest.php new file mode 100644 index 0000000..49a0689 --- /dev/null +++ b/tests/Tests/Clauses/WhereInTest.php @@ -0,0 +1,25 @@ +whereInJoin('seller.id', [1, 2]) + ->get(); + + $queryTest = 'select orders.* + from "orders" + left join "sellers" on "sellers"."id" = "orders"."seller_id" + where "sellers"."id" in (?, ?) + and "orders"."deleted_at" is null + group by "orders"."id"'; + + $this->assertQueryMatches($queryTest, $this->fetchQuery()); + } +} diff --git a/tests/Tests/Clauses/WhereNotInTest.php b/tests/Tests/Clauses/WhereNotInTest.php new file mode 100644 index 0000000..02bcd9b --- /dev/null +++ b/tests/Tests/Clauses/WhereNotInTest.php @@ -0,0 +1,25 @@ +whereNotInJoin('seller.id', [1, 2]) + ->get(); + + $queryTest = 'select orders.* + from "orders" + left join "sellers" on "sellers"."id" = "orders"."seller_id" + where "sellers"."id" not in (?, ?) + and "orders"."deleted_at" is null + group by "orders"."id"'; + + $this->assertQueryMatches($queryTest, $this->fetchQuery()); + } +}