laravel-eloquent-query-cache icon indicating copy to clipboard operation
laravel-eloquent-query-cache copied to clipboard

ManyToMany invalidate cache is not bi-directional when attaching/detaching

Open antoscarface opened this issue 2 years ago • 1 comments

Hi,

I'm using your library to cache entities with Eloquent and I also read this in order to invalidate cache also with ManyToMany relationships, using pivot tables.

I installed chelout/laravel-relationship-events and added the traits, it works in one direction and not in the other. I explain with an example:

Given these models:

class ModelA extends Model {
    use HasBelongsToManyEvents;
    use HasRelationshipObservables;
    use QueryCacheable;

    protected static bool $flushCacheOnUpdate = true;

    public function modelBs(): BelongsToMany
    {
        return $this->belongsToMany(ModelB::class, 'model_a_b')
            ->using(ModelABPivot::class);
    }
}

...

class ModelB extends Model {
    use HasBelongsToManyEvents;
    use HasRelationshipObservables;
    use QueryCacheable;

    protected static bool $flushCacheOnUpdate = true;

    public function modelAs(): BelongsToMany
    {
        return $this->belongsToMany(ModelA::class, 'model_a_b')
            ->using(ModelABPivot::class);
    }
}

...

class ModelABPivot extends Pivot {
}

When I run $modelA->modelBs()->attach($modelB):

  • $modelB->modelAs() returns the $modelA attached
  • $modelA->modelBs() does not

I do need ModelA::flushCache() to make it work.

Same thing vice-versa when using detach.

What's wrong with what I did? Could you please help me?

antoscarface avatar Aug 17 '22 15:08 antoscarface

It doesn't work even if I add the cache trait in the pivot table class:

class ModelABPivot extends Pivot {
    use QueryCacheable;

    protected static bool $flushCacheOnUpdate = true;
}

antoscarface avatar Aug 17 '22 15:08 antoscarface

This issue has been automatically closed because it has not had any recent activity. 😨

stale[bot] avatar Sep 30 '22 23:09 stale[bot]