Skip to content

Commit b7a3cc6

Browse files
authored
Merge pull request #13 from GeneaLabs/laravel-5.5
Laravel 5.5
2 parents ce2f023 + 0e0add1 commit b7a3cc6

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.6] - 2017-10-12
8+
### Added
9+
- orderBy clause to cache key. Thanks @RobMKR for the PR!
10+
711
## [0.2.5] - 2017-10-04
812
### Fixed
913
- parsing of nested, exists, raw, and column where clauses.

src/CachedBuilder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected function getCacheKey(array $columns = ['*'], $idColumn = null) : strin
2727
$key .= $this->getQueryColumns($columns);
2828
$key .= $this->getWhereClauses();
2929
$key .= $this->getWithModels();
30+
$key .= $this->getOrderByClauses();
3031
$key .= $this->getOffsetClause();
3132
$key .= $this->getLimitClause();
3233

@@ -117,6 +118,14 @@ protected function getWithModels() : string
117118
return '-' . implode('-', $eagerLoads->keys()->toArray());
118119
}
119120

121+
protected function getOrderByClauses(){
122+
$orders = collect($this->query->orders);
123+
124+
return $orders->reduce(function($carry, $order){
125+
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
126+
});
127+
}
128+
120129
protected function getCacheTags() : array
121130
{
122131
return collect($this->eagerLoad)->keys()

tests/Unit/CachedBuilderTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public function testChunkModelResultsCreatesCache()
227227
}
228228

229229
$cachedChunks['authors']->push($chunk);
230-
$cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile{$offset}-limit_3");
230+
$cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile_orderBy_authors.id_asc{$offset}-limit_3");
231231
});
232232

233233
(new UncachedAuthor)->with('books', 'profile')
@@ -464,6 +464,22 @@ public function testLazyLoadingOnResourceIsCached()
464464
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
465465
}
466466

467+
public function testOrderByClauseParsing()
468+
{
469+
$authors = (new Author)->orderBy('name')->get();
470+
471+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderBy_name_asc';
472+
$tags = [
473+
'genealabslaravelmodelcachingtestsfixturesauthor',
474+
];
475+
476+
$cachedResults = cache()->tags($tags)->get($key);
477+
$liveResults = (new UncachedAuthor)->orderBy('name')->get();
478+
479+
$this->assertEmpty($authors->diffAssoc($cachedResults));
480+
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
481+
}
482+
467483
public function testNestedRelationshipWhereClauseParsing()
468484
{
469485
// -> with('modelA.modelB')

0 commit comments

Comments
 (0)