Skip to content

Commit df52b65

Browse files
committed
Fixed generation of cache key where clauses
Fixes #244
1 parent b075e9a commit df52b65

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/CacheKey.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected function getValuesFromWhere(array $where) : string
158158

159159
protected function getValuesFromBindings(array $where, string $values) : string
160160
{
161-
if ($this->query->bindings["where"][$this->currentBinding] ?? false) {
161+
if (($this->query->bindings["where"][$this->currentBinding] ?? false) !== false) {
162162
$values = $this->query->bindings["where"][$this->currentBinding];
163163
$this->currentBinding++;
164164

@@ -168,7 +168,7 @@ protected function getValuesFromBindings(array $where, string $values) : string
168168
}
169169
}
170170

171-
return $values ?: "";
171+
return $values;
172172
}
173173

174174
protected function getWhereClauses(array $wheres = []) : string

tests/Integration/CachedBuilder/WhereRawTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,27 @@ public function testWhereRawParametersCacheUniquely()
123123
$this->assertEquals($cachedBook1->first()->title, $result1->first()->title);
124124
$this->assertEquals($cachedBook2->first()->title, $result2->first()->title);
125125
}
126+
127+
public function testWhereRawParametersAfterWhereClause()
128+
{
129+
$book1 = (new UncachedBook)->first();
130+
$book2 = (new UncachedBook)->orderBy("id", "DESC")->first();
131+
132+
$result1 = (new Book)
133+
->where("id", ">", 0)
134+
->whereRaw("id = ?", [$book1->id])
135+
->get();
136+
$result2 = (new Book)
137+
->where("id", ">", 1)
138+
->whereRaw("id = ?", [$book2->id])
139+
->get();
140+
$key1 = sha1("genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-id_>_0-_and_id_=_{$book1->id}");
141+
$key2 = sha1("genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-id_>_1-_and_id_=_{$book2->id}");
142+
$tags = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook'];
143+
$cachedBook1 = $this->cache()->tags($tags)->get($key1)['value'];
144+
$cachedBook2 = $this->cache()->tags($tags)->get($key2)['value'];
145+
146+
$this->assertEquals($cachedBook1->first()->title, $result1->first()->title);
147+
$this->assertEquals($cachedBook2->first()->title, $result2->first()->title);
148+
}
126149
}

tests/Integration/CachedBuilder/WhereTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testWhereClauseParsingOfOperators()
9898

9999
public function testTwoWhereClausesAfterEachOther()
100100
{
101-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-id_>_-id_<_100');
101+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-id_>_0-id_<_100');
102102
$tags = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor'];
103103

104104
$authors = (new Author)

0 commit comments

Comments
 (0)