Skip to content

Commit 687568e

Browse files
authored
Merge pull request #138 from GeneaLabs/feature/fix-morph-many-not-busting-cache
WIP - Fix MorphMany Not Busting Cache
2 parents 17b905c + 067bdd5 commit 687568e

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

src/Traits/ModelCaching.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public static function all($columns = ['*'])
2525

2626
public static function bootCachable()
2727
{
28+
static::created(function ($instance) {
29+
$instance->checkCooldownAndFlushAfterPersiting($instance);
30+
});
31+
2832
static::deleted(function ($instance) {
2933
$instance->checkCooldownAndFlushAfterPersiting($instance);
3034
});
@@ -33,6 +37,11 @@ public static function bootCachable()
3337
$instance->checkCooldownAndFlushAfterPersiting($instance);
3438
});
3539

40+
// TODO: figure out how to add this listener
41+
// static::restored(function ($instance) {
42+
// $instance->checkCooldownAndFlushAfterPersiting($instance);
43+
// });
44+
3645
static::pivotAttached(function ($instance) {
3746
$instance->checkCooldownAndFlushAfterPersiting($instance);
3847
});

tests/Fixtures/Book.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
44
use Illuminate\Database\Eloquent\Model;
55
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
use Illuminate\Database\Eloquent\Relations\MorphMany;
67
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
78

89
class Book extends Model
@@ -27,6 +28,11 @@ public function author() : BelongsTo
2728
return $this->belongsTo(Author::class);
2829
}
2930

31+
public function comments() : MorphMany
32+
{
33+
return $this->morphMany(Comment::class, "commentable");
34+
}
35+
3036
public function publisher() : BelongsTo
3137
{
3238
return $this->belongsTo(Publisher::class);

tests/Fixtures/Comment.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\MorphTo;
6+
7+
class Comment extends Model
8+
{
9+
use Cachable;
10+
11+
protected $fillable = [
12+
'description',
13+
'subject',
14+
];
15+
16+
public function commentable() : MorphTo
17+
{
18+
return $this->morphTo();
19+
}
20+
}

tests/Fixtures/UncachedComment.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\MorphTo;
6+
7+
class UncachedComment extends Model
8+
{
9+
protected $fillable = [
10+
'description',
11+
'subject',
12+
];
13+
protected $table = "comments";
14+
15+
public function commentable() : MorphTo
16+
{
17+
return $this->morphTo();
18+
}
19+
}

tests/Integration/CachedBuilder/WithCountTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
44
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Comment;
56
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
67
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
78
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
89
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
910
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
11+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedComment;
1012
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile;
1113
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher;
1214
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore;
@@ -39,4 +41,24 @@ public function testWithCountUpdatesAfterRecordIsAdded()
3941
$this->assertNotEquals($author1->books_count, $author2->books_count);
4042
$this->assertEquals($author1->books_count + 1, $author2->books_count);
4143
}
44+
45+
public function testWithCountOnMorphManyRelationshipUpdatesAfterRecordIsAdded()
46+
{
47+
$book1 = (new Book)
48+
->withCount("comments")
49+
->first();
50+
$comment = factory(Comment::class, 1)
51+
->create()
52+
->first();
53+
54+
$book1->comments()->save($comment);
55+
56+
$book2 = (new Book)
57+
->withCount("comments")
58+
->where("id", $book1->id)
59+
->first();
60+
61+
$this->assertNotEquals($book1->comments_count, $book2->comments_count);
62+
$this->assertEquals($book1->comments_count + 1, $book2->comments_count);
63+
}
4264
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Comment;
5+
6+
$factory->define(Comment::class, function (Faker $faker) {
7+
return [
8+
'description' => $faker->paragraphs(3, true),
9+
'subject' => $faker->sentence,
10+
];
11+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateComments extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('comments', function (Blueprint $table) {
11+
$table->increments('id');
12+
$table->timestamps();
13+
14+
$table->unsignedInteger("commentable_id")->nullable();
15+
$table->string("commentable_type")->nullable();
16+
$table->text("description");
17+
$table->string("subject");
18+
});
19+
}
20+
21+
public function down()
22+
{
23+
//
24+
}
25+
}

0 commit comments

Comments
 (0)