Skip to content

Commit 7b8bb58

Browse files
committed
Fixed BelongsToMany relationship to not cache if the target model is not also cachable
1 parent cd898c5 commit 7b8bb58

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
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.4.24] - 18 May 2019
8+
### Fixed
9+
- BelongsToMany relationship to not cache if the target model is not also cachable.
10+
711
## [0.4.23] - 18 May 2019
812
### Added
913
- tests for lazy-loading the following relationships:

src/Traits/ModelCaching.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GeneaLabs\LaravelModelCaching\CachedBuilder;
66
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
77
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
89

910
trait ModelCaching
1011
{
@@ -89,7 +90,22 @@ protected function newBelongsToMany(
8990
$relatedKey,
9091
$relationName = null
9192
) {
92-
return new CachedBelongsToMany(
93+
if (method_exists($query->getModel(), "isCachable")
94+
&& $query->getModel()->isCachable()
95+
) {
96+
return new CachedBelongsToMany(
97+
$query,
98+
$parent,
99+
$table,
100+
$foreignPivotKey,
101+
$relatedPivotKey,
102+
$parentKey,
103+
$relatedKey,
104+
$relationName
105+
);
106+
}
107+
108+
return new BelongsToMany(
93109
$query,
94110
$parent,
95111
$table,

tests/Fixtures/Book.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ public function stores() : BelongsToMany
4848
{
4949
return $this->belongsToMany(Store::class);
5050
}
51+
52+
public function uncachedStores() : BelongsToMany
53+
{
54+
return $this->belongsToMany(UncachedStore::class, "book_store", "book_id", "store_id");
55+
}
5156
}

tests/Integration/CachedBuilder/BelongsToManyTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,35 @@ public function testInvalidatingCacheWhenUpdating()
128128
$this->assertNotEmpty($result);
129129
$this->assertNull($cachedResult);
130130
}
131+
132+
public function testUncachedRelatedModelDoesntCache()
133+
{
134+
$bookId = (new Store)
135+
->disableModelCaching()
136+
->with("books")
137+
->first()
138+
->books
139+
->first()
140+
->id;
141+
$key = sha1("genealabs:laravel-model-caching:testing::memory::book-store:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}");
142+
$tags = [
143+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesuncachedstore',
144+
];
145+
146+
$result = (new Book)
147+
->find($bookId)
148+
->uncachedStores;
149+
$cachedResult = $this
150+
->cache()
151+
->tags($tags)
152+
->get($key)['value'];
153+
$uncachedResult = (new UncachedBook)
154+
->find($bookId)
155+
->stores;
156+
157+
$this->assertEquals($uncachedResult->pluck("id"), $result->pluck("id"));
158+
$this->assertNull($cachedResult);
159+
$this->assertNotNull($result);
160+
$this->assertNotNull($uncachedResult);
161+
}
131162
}

0 commit comments

Comments
 (0)