mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.23k stars 212 forks source link

MorphToMany relation not being invalidated #379

Open denitsa-md opened 3 years ago

denitsa-md commented 3 years ago

First off, thank you for the package!

I'm not sure if I'm doing this wrong or it's simply not supported but here it is:

Describe the bug I have a Subscription model that has a morphToMany relation to setting keys (so a many to many polymorphic). The Subscription model is not cacheable and the relation to setting keys looks like this. The Setting model (the pivot) is also not cacheable.

    public function settingKeys(): MorphToMany
    {
        return $this
            ->morphToMany(SettingKey::class, 'settingable', 'settings')
            ->using(Setting::class)
            ->withPivot('value')
            ->withTimestamps();
    }

The SettingKey model on the other hand usesCachable.

Eloquent Query

This test will fail with Failed asserting that 1 matches expected 0. at the last line:

        ...
        $subscriptionPeriod->settingKeys()->attach($settingKey->id, ['value' => 'something']);

        $this->assertEquals(1, $subscriptionPeriod->settingKeys()->count());

        $subscriptionPeriod->settingKeys()->detach();

        $this->assertEquals(0, $subscriptionPeriod->settingKeys()->count());

This test with disableCache will pass:

        ...
        $subscriptionPeriod->settingKeys()->attach($settingKey->id, ['value' => 'something']);

        $this->assertEquals(1, $subscriptionPeriod->settingKeys()->count());

        $subscriptionPeriod->settingKeys()->detach();

        $this->assertEquals(0, $subscriptionPeriod->settingKeys()->disableCache()->count());

I'm using the array driver for testing. (We do have the file driver in production although it's not officially supported anymore.. I hope to be switching it over to redis soon as we'll need to cache some more things).

Stack Trace ....

Environment