From 91f92f23a7e2a4a801c1bd67f9af6137efdd62d7 Mon Sep 17 00:00:00 2001 From: dmason30 Date: Thu, 9 Apr 2020 02:43:46 +0100 Subject: [PATCH] Handle json contains with array value --- src/CacheKey.php | 4 ++++ .../CachedBuilder/WhereJsonContainsTest.php | 23 +++++++++++++++++++ tests/database/factories/AuthorFactory.php | 1 + 3 files changed, 28 insertions(+) diff --git a/src/CacheKey.php b/src/CacheKey.php index 025b431..cef259f 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -164,6 +164,10 @@ protected function getValuesFromWhere(array $where) : string return implode("_", collect($where["values"])->flatten()->toArray()); } + if (is_array((new Arr)->get($where, "value"))) { + return implode("_", collect($where["value"])->flatten()->toArray()); + } + return (new Arr)->get($where, "value", ""); } diff --git a/tests/Integration/CachedBuilder/WhereJsonContainsTest.php b/tests/Integration/CachedBuilder/WhereJsonContainsTest.php index aab68ec..71ebcf8 100644 --- a/tests/Integration/CachedBuilder/WhereJsonContainsTest.php +++ b/tests/Integration/CachedBuilder/WhereJsonContainsTest.php @@ -49,4 +49,27 @@ public function testWithInUsingCollectionQuery() $this->assertEquals($liveResults->pluck("id"), $authors->pluck("id")); $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id")); } + + public function testWithInUsingCollectionQueryWithArrayValues() + { + $key = sha1("genealabs:laravel-model-caching:pgsql:testing:authors:genealabslaravelmodelcachingtestsfixturesauthor-finances->tags_jsoncontains_[\"foo\",\"bar\"]-authors.deleted_at_null"); + $tags = [ + 'genealabs:laravel-model-caching:pgsql:testing:genealabslaravelmodelcachingtestsfixturesauthor', + ]; + + $authors = (new Author) + ->whereJsonContains("finances->tags", ['foo', 'bar']) + ->get(); + $liveResults = (new UncachedAuthor) + ->whereJsonContains("finances->tags", ['foo', 'bar']) + ->get(); + + $cachedResults = $this + ->cache() + ->tags($tags) + ->get($key)['value']; + + $this->assertEquals($liveResults->pluck("id"), $authors->pluck("id")); + $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id")); + } } diff --git a/tests/database/factories/AuthorFactory.php b/tests/database/factories/AuthorFactory.php index cd26488..a88e002 100644 --- a/tests/database/factories/AuthorFactory.php +++ b/tests/database/factories/AuthorFactory.php @@ -11,6 +11,7 @@ "total" => 5000, "weekly" => 100, "daily" => 20, + 'tags' => ['foo', 'bar'], ], "is_famous" => $faker->boolean(), ];