Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MorphToMany relation not being invalidated #379

Open
denitsa-md opened this issue Oct 12, 2020 · 1 comment
Open

MorphToMany relation not being invalidated #379

denitsa-md opened this issue Oct 12, 2020 · 1 comment
Assignees

Comments

@denitsa-md
Copy link

denitsa-md commented Oct 12, 2020

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 uses Cachable.

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

  • PHP: 7.4
  • OS: Windows 10
  • Laravel: 7.28.3
  • Model Caching: 0.10.2
@beshkin
Copy link

beshkin commented Jul 25, 2024

Same issue with HasManyThrough.
Seems like intermediate model is not supported.

class User extends Model {
    public function comments(): HasManyThrough
    {
        return $this->hasManyThrough(
            Comment::class,
            UsersComment::class,
            'user_id',
            'id',
            'id',
            'comment_id',
        );
    }
}

Comment is cached.
User and UsersComment - not cached.
Adding new sources to the intermediate table users_comment doesn't invalidate cache for comment
The following will fail at any time, unless I clear the cache manually:

$this->assertEquals($user->comments()->disableCache()->count(), $user->comments()->count());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants