From 774b2d38bd2750f28ebf218cd32123e9af05e6ce Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 5 Dec 2017 07:55:27 -0800 Subject: [PATCH 1/2] Fix key generation on `orderByRaw`, Fixes #35 --- src/CacheKey.php | 11 ++++++++--- tests/Unit/CachedBuilderTest.php | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/CacheKey.php b/src/CacheKey.php index d5d2fad..b5eee6b 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -63,9 +63,14 @@ protected function getOrderByClauses() : string { $orders = collect($this->query->orders); - return $orders->reduce(function ($carry, $order) { - return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction']; - }) + return $orders + ->reduce(function ($carry, $order) { + if ($order['type'] === 'Raw') { + return $carry . '_orderByRaw_' . str_slug($order['sql']); + } + + return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction']; + }) ?: ''; } diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index dbbe3bc..4fe23cb 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -639,4 +639,25 @@ public function testRelationshipQueriesAreCached() $this->assertTrue($cachedResults->diffAssoc($books)->isEmpty()); $this->assertTrue($liveResults->diffAssoc($books)->isEmpty()); } + + public function testRawOrderByWithoutColumnReference() + { + $authors = (new Author) + ->orderByRaw('DATE()') + ->get(); + + $key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderByRaw_date'; + $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; + + $cachedResults = cache() + ->tags($tags) + ->get($key); + + $liveResults = (new UncachedAuthor) + ->orderByRaw('DATE()') + ->get(); + + $this->assertTrue($cachedResults->diffAssoc($authors)->isEmpty()); + $this->assertTrue($liveResults->diffAssoc($authors)->isEmpty()); + } } From 140e3c71f97852d0092ab4025fc669c3ae7f0df7 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 5 Dec 2017 08:01:16 -0800 Subject: [PATCH 2/2] Fix inspection of `type` attribute --- src/CacheKey.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CacheKey.php b/src/CacheKey.php index b5eee6b..9e3cd50 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -65,7 +65,7 @@ protected function getOrderByClauses() : string return $orders ->reduce(function ($carry, $order) { - if ($order['type'] === 'Raw') { + if (($order['type'] ?? '') === 'Raw') { return $carry . '_orderByRaw_' . str_slug($order['sql']); }