Skip to content

Commit

Permalink
Merge pull request #36 from GeneaLabs/laravel-5.5
Browse files Browse the repository at this point in the history
Fix key generation on `orderByRaw`
  • Loading branch information
mikebronner authored Dec 5, 2017
2 parents 35ba21d + 140e3c7 commit 0cf6758
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
})
?: '';
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/CachedBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 0cf6758

Please sign in to comment.