From b0efb6603a4c822824cc725b4673f5835db3dd19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktoras=20Mork=C5=ABnas?= Date: Mon, 9 Jul 2018 14:55:46 +0300 Subject: [PATCH 1/2] Fix nested where binding issue --- src/CacheKey.php | 2 -- tests/Integration/CachedBuilder/WhereRawTest.php | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/CacheKey.php b/src/CacheKey.php index fde63ca..9b46fc7 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -164,8 +164,6 @@ protected function getNestedClauses(array $where) : string return ""; } - $this->currentBinding++; - return "-" . strtolower($where["type"]) . $this->getWhereClauses($where["query"]->wheres); } diff --git a/tests/Integration/CachedBuilder/WhereRawTest.php b/tests/Integration/CachedBuilder/WhereRawTest.php index ca30538..48a3acb 100644 --- a/tests/Integration/CachedBuilder/WhereRawTest.php +++ b/tests/Integration/CachedBuilder/WhereRawTest.php @@ -89,4 +89,19 @@ public function testNestedWhereRawClauses() $this->assertEquals($expectedIds, $authors->pluck("id")->toArray()); } + + public function testNestedWhereRawWithBindings() + { + $books = (new Book) + ->where(function ($query) { + $query->whereRaw("title like ? or description like ? or published_at like ? or price like ?", ['%larravel%', '%larravel%', '%larravel%', '%larravel%',]); + })->get(); + + $uncachedBooks = (new UncachedBook) + ->where(function ($query) { + $query->whereRaw("title like ? or description like ? or published_at like ? or price like ?", ['%larravel%', '%larravel%', '%larravel%', '%larravel%',]); + })->get(); + + $this->assertEquals($books->pluck("id"), $uncachedBooks->pluck("id")); + } } From 55a4f345b8d08c217c3fd5d3545a8f16a5e8bee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktoras=20Mork=C5=ABnas?= Date: Wed, 11 Jul 2018 13:35:16 +0300 Subject: [PATCH 2/2] Fix whereHas query with nested whereRaw --- composer.json | 2 +- src/CacheKey.php | 2 -- .../CachedBuilderRelationshipsTest.php | 19 +++++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 2f73d4f..c815f10 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "sebastian/phpcpd": "*", "symfony/thanks": "*", "predis/predis": "*", - "codedungeon/phpunit-result-printer": "*" + "codedungeon/phpunit-result-printer": "^0.19.10" }, "autoload": { "psr-4": { diff --git a/src/CacheKey.php b/src/CacheKey.php index 9b46fc7..9ddc9a0 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -173,8 +173,6 @@ protected function getColumnClauses(array $where) : string return ""; } - $this->currentBinding++; - return "-{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}"; } diff --git a/tests/Integration/CachedBuilderRelationshipsTest.php b/tests/Integration/CachedBuilderRelationshipsTest.php index 86184e1..22aa5dc 100644 --- a/tests/Integration/CachedBuilderRelationshipsTest.php +++ b/tests/Integration/CachedBuilderRelationshipsTest.php @@ -38,4 +38,23 @@ public function testHasRelationshipResults() $this->assertNotEmpty($booksWithStores); $this->assertEquals($booksWithStores, $cachedResults); } + + public function testWhereHasRelationship() + { + $books = (new Book) + ->with("stores") + ->whereHas("stores", function ($query) { + $query->whereRaw('address like ?', ['%s%']); + }) + ->get(); + + $uncachedBooks = (new UncachedBook) + ->with("stores") + ->whereHas("stores", function ($query) { + $query->whereRaw('address like ?', ['%s%']); + }) + ->get(); + + $this->assertEquals($books->pluck("id"), $uncachedBooks->pluck("id")); + } }