diff --git a/CHANGELOG.md b/CHANGELOG.md index 21be263..608d21f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [0.2.6] - 2017-10-12 +### Added +- orderBy clause to cache key. Thanks @RobMKR for the PR! + ## [0.2.5] - 2017-10-04 ### Fixed - parsing of nested, exists, raw, and column where clauses. diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index f01b59c..6e6de17 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -27,6 +27,7 @@ protected function getCacheKey(array $columns = ['*'], $idColumn = null) : strin $key .= $this->getQueryColumns($columns); $key .= $this->getWhereClauses(); $key .= $this->getWithModels(); + $key .= $this->getOrderByClauses(); $key .= $this->getOffsetClause(); $key .= $this->getLimitClause(); @@ -117,6 +118,14 @@ protected function getWithModels() : string return '-' . implode('-', $eagerLoads->keys()->toArray()); } + protected function getOrderByClauses(){ + $orders = collect($this->query->orders); + + return $orders->reduce(function($carry, $order){ + return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction']; + }); + } + protected function getCacheTags() : array { return collect($this->eagerLoad)->keys() diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 99a30ff..1a995df 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -227,7 +227,7 @@ public function testChunkModelResultsCreatesCache() } $cachedChunks['authors']->push($chunk); - $cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile{$offset}-limit_3"); + $cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile_orderBy_authors.id_asc{$offset}-limit_3"); }); (new UncachedAuthor)->with('books', 'profile') @@ -464,6 +464,22 @@ public function testLazyLoadingOnResourceIsCached() $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } + public function testOrderByClauseParsing() + { + $authors = (new Author)->orderBy('name')->get(); + + $key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderBy_name_asc'; + $tags = [ + 'genealabslaravelmodelcachingtestsfixturesauthor', + ]; + + $cachedResults = cache()->tags($tags)->get($key); + $liveResults = (new UncachedAuthor)->orderBy('name')->get(); + + $this->assertEmpty($authors->diffAssoc($cachedResults)); + $this->assertEmpty($liveResults->diffAssoc($cachedResults)); + } + public function testNestedRelationshipWhereClauseParsing() { // -> with('modelA.modelB')