Skip to content

Commit 774b2d3

Browse files
committed
Fix key generation on orderByRaw, Fixes #35
1 parent dd75dc6 commit 774b2d3

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/CacheKey.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ protected function getOrderByClauses() : string
6363
{
6464
$orders = collect($this->query->orders);
6565

66-
return $orders->reduce(function ($carry, $order) {
67-
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
68-
})
66+
return $orders
67+
->reduce(function ($carry, $order) {
68+
if ($order['type'] === 'Raw') {
69+
return $carry . '_orderByRaw_' . str_slug($order['sql']);
70+
}
71+
72+
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
73+
})
6974
?: '';
7075
}
7176

tests/Unit/CachedBuilderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,4 +639,25 @@ public function testRelationshipQueriesAreCached()
639639
$this->assertTrue($cachedResults->diffAssoc($books)->isEmpty());
640640
$this->assertTrue($liveResults->diffAssoc($books)->isEmpty());
641641
}
642+
643+
public function testRawOrderByWithoutColumnReference()
644+
{
645+
$authors = (new Author)
646+
->orderByRaw('DATE()')
647+
->get();
648+
649+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderByRaw_date';
650+
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
651+
652+
$cachedResults = cache()
653+
->tags($tags)
654+
->get($key);
655+
656+
$liveResults = (new UncachedAuthor)
657+
->orderByRaw('DATE()')
658+
->get();
659+
660+
$this->assertTrue($cachedResults->diffAssoc($authors)->isEmpty());
661+
$this->assertTrue($liveResults->diffAssoc($authors)->isEmpty());
662+
}
642663
}

0 commit comments

Comments
 (0)