Skip to content

Commit fb55d1b

Browse files
committed
Fix subquery parsing for NotSubIn and SubIn
1 parent d0c3471 commit fb55d1b

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/CacheKey.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function getQueryColumns(array $columns) : string
9797

9898
protected function getTypeClause($where) : string
9999
{
100-
$type =in_array($where["type"], ["In", "NotIn", "Null", "NotNull", "between"])
100+
$type = in_array($where["type"], ["In", "NotIn", "Null", "NotNull", "between", "NotInSub", "InSub"])
101101
? strtolower($where["type"])
102102
: strtolower($where["operator"]);
103103

@@ -120,6 +120,18 @@ protected function getValuesClause(array $where = null) : string
120120

121121
protected function getValuesFromWhere(array $where) : string
122122
{
123+
if (array_get($where, "query")) {
124+
$prefix = $this->getCachePrefix();
125+
$subKey = (new self($this->eagerLoad, $this->model, $where["query"]))
126+
->make();
127+
$subKey = str_replace($prefix, "", $subKey);
128+
$subKey = str_replace($this->getModelSlug(), "", $subKey);
129+
$classParts = explode("\\", get_class($this->model));
130+
$subKey = strtolower(array_pop($classParts)) . $subKey;
131+
132+
return $subKey;
133+
}
134+
123135
if (is_array(array_get($where, "values"))) {
124136
return implode("_", $where["values"]);
125137
}

tests/Integration/CachedBuilder/WhereNotInTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
class WhereNotInTest extends IntegrationTestCase
1919
{
20-
21-
2220
public function testWhereNotInQuery()
2321
{
2422
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook-author_id_not_in_1_2_3_4');
@@ -64,4 +62,30 @@ public function testWhereNotInResults()
6462
$this->assertEquals($liveResults->pluck("id"), $results->pluck("id"));
6563
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
6664
}
65+
66+
public function testWhereNotInSubquery()
67+
{
68+
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook-id_notinsub_book-id_<_10');
69+
$tags = [
70+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
71+
];
72+
73+
$results = (new Book)
74+
->whereNotIn("id", function ($query) {
75+
$query->select("id")->from("authors")->where("id", "<", 10);
76+
})
77+
->get();
78+
$cachedResults = $this
79+
->cache()
80+
->tags($tags)
81+
->get($key)['value'];
82+
$liveResults = (new UncachedBook)
83+
->whereNotIn("id", function ($query) {
84+
$query->select("id")->from("authors")->where("id", "<", 10);
85+
})
86+
->get();
87+
88+
$this->assertEquals($liveResults->pluck("id"), $results->pluck("id"));
89+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
90+
}
6791
}

0 commit comments

Comments
 (0)