Skip to content

Commit 32a6e67

Browse files
committed
Fix binding resolution for whereIn queries
1 parent e1cb6da commit 32a6e67

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/CacheKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ protected function getInAndNotInClauses(array $where) : string
225225
$type = strtolower($where["type"]);
226226
$subquery = $this->getValuesFromWhere($where);
227227
$values = collect($this->query->bindings["where"][$this->currentBinding] ?? []);
228-
$this->currentBinding++;
228+
$this->currentBinding += count($where["values"]);
229229
$subquery = collect(vsprintf(str_replace("?", "%s", $subquery), $values->toArray()));
230230
$values = $this->recursiveImplode($subquery->toArray(), "_");
231231

tests/Integration/CachedBuilder/WhereInTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,26 @@ public function testBindingsAreCorrectWithMultipleWhereInClauses()
7878
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
7979
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
8080
}
81+
82+
/** @group test */
83+
public function testWhereInUsesCorrectBindings()
84+
{
85+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-id_in_1_2_3_4_5-id_between_1_99999');
86+
$tags = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor'];
87+
88+
$authors = (new Author)
89+
->whereIn('id', [1,2,3,4,5])
90+
->whereBetween('id', [1, 99999])
91+
->get();
92+
$cachedResults = $this->cache()
93+
->tags($tags)
94+
->get($key)['value'];
95+
$liveResults = (new UncachedAuthor)
96+
->whereIn('id', [1,2,3,4,5])
97+
->whereBetween('id', [1, 99999])
98+
->get();
99+
100+
$this->assertEmpty($authors->diffKeys($cachedResults));
101+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
102+
}
81103
}

tests/Integration/CachedBuilder/WhereTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,29 @@ public function testTwoWhereClausesAfterEachOther()
116116
$this->assertEmpty($authors->diffKeys($cachedResults));
117117
$this->assertEmpty($liveResults->diffKeys($cachedResults));
118118
}
119+
120+
public function testWhereUsesCorrectBinding()
121+
{
122+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-id_>_5-name_like_B%');
123+
$tags = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor'];
124+
125+
$authors = (new Author)
126+
->where("id", ">", 2)
127+
->where("name", "LIKE", "A%")
128+
->get();
129+
$authors = (new Author)
130+
->where("id", ">", 5)
131+
->where("name", "LIKE", "B%")
132+
->get();
133+
$cachedResults = $this->cache()
134+
->tags($tags)
135+
->get($key)['value'];
136+
$liveResults = (new UncachedAuthor)
137+
->where("id", ">", 5)
138+
->where("name", "LIKE", "B%")
139+
->get();
140+
141+
$this->assertEmpty($authors->diffKeys($cachedResults));
142+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
143+
}
119144
}

0 commit comments

Comments
 (0)