Skip to content

Commit c71d443

Browse files
committed
Fix where clauses using DateTime
1 parent 875cc9c commit c71d443

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
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.5.1] - 21 May 2019
8+
### Fixed
9+
- caching of where clauses using DateTime instead of Carbon.
10+
711
## [0.5.0] - 20 May 2019
812
### Changed
913
- implementation of model-based cache prefix.

src/CacheKey.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ protected function getValuesClause(array $where = []) : string
147147

148148
protected function getValuesFromWhere(array $where) : string
149149
{
150+
if (array_key_exists("value", $where)
151+
&& is_object($where["value"])
152+
&& get_class($where["value"]) === "DateTime"
153+
) {
154+
return $where["value"]->format("Y-m-d-H-i-s");
155+
}
156+
150157
if (is_array((new Arr)->get($where, "values"))) {
151158
return implode("_", collect($where["values"])->flatten()->toArray());
152159
}
@@ -166,6 +173,12 @@ protected function getValuesFromBindings(array $where, string $values) : string
166173
}
167174
}
168175

176+
if (is_object($values)
177+
&& get_class($values) === "DateTime"
178+
) {
179+
$values = $values->format("Y-m-d-H-i-s");
180+
}
181+
169182
return $values;
170183
}
171184

tests/Integration/CachedBuilder/DateTimeTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
22

3+
use DateInterval;
4+
use DateTime;
35
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
46
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
57
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
68

79
class DateTimeTest extends IntegrationTestCase
810
{
9-
public function testDateWhereCreatesCorrectCacheKey()
11+
public function testWhereClauseWorksWithCarbonDate()
1012
{
11-
$dateTime = now()->subYears(10)->toDateString();
13+
$dateTime = now()->subYears(10);
1214
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-publish_at_>_{$dateTime}");
1315
$tags = [
1416
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
@@ -30,4 +32,31 @@ public function testDateWhereCreatesCorrectCacheKey()
3032
$this->assertNotEmpty($cachedResults);
3133
$this->assertNotEmpty($liveResults);
3234
}
35+
36+
public function testWhereClauseWorksWithDateTimeObject()
37+
{
38+
$dateTime = (new DateTime('@' . time()))
39+
->sub(new DateInterval("P10Y"));
40+
$dateTimeString = $dateTime->format("Y-m-d-H-i-s");
41+
$key = sha1("genealabs:laravel-model-caching:testing:/Users/mike/Developer/Sites/laravel-model-caching/tests/database/testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-publish_at_>_{$dateTimeString}");
42+
$tags = [
43+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
44+
];
45+
46+
$results = (new Book)
47+
->where("publish_at", ">", $dateTime)
48+
->get();
49+
$cachedResults = $this->cache()
50+
->tags($tags)
51+
->get($key)['value'];
52+
$liveResults = (new UncachedBook)
53+
->where("publish_at", ">", $dateTime)
54+
->get();
55+
56+
$this->assertEquals($liveResults->pluck("id"), $results->pluck("id"));
57+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
58+
$this->assertNotEmpty($results);
59+
$this->assertNotEmpty($cachedResults);
60+
$this->assertNotEmpty($liveResults);
61+
}
3362
}

tests/database/baseline.sqlite

0 Bytes
Binary file not shown.

tests/database/testing.sqlite

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)