mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.26k stars 217 forks source link

Incorrect results for join queries #102

Closed sjbronner closed 6 years ago

sjbronner commented 6 years ago

Issue

Certain queries receive incorrect results. We were guessing it might have to do with inner joins. Here are two queries from the ceed project:

select * from "events" where "venue_id" in ('2') and exists (select * from "contacts" inner join "event_musician" on "contacts"."id" = "event_musician"."contact_id" where "events"."id" = "event_musician"."event_id")
select "contacts".*, "event_musician"."event_id" as "pivot_event_id", "event_musician"."contact_id" as "pivot_contact_id" from "contacts" inner join "event_musician" on "contacts"."id" = "event_musician"."contact_id" where "event_musician"."event_id" = '251'

Environment

Laravel Version: v5.5.39 Laravel Model Caching Package Version: 0.2.51 PHP Version: 7.2.3 Operating System & Version: Archlinux

mikebronner commented 6 years ago

Hi @sjbronner, can you also provide the Eloquent queries that go along with these SQL queries? I would like to recreate a test in the same way your Queries are constructed. Thanks!

sjbronner commented 6 years ago

Scope in Event Model:

    public function scopeHasMusician(Builder $query) : Builder                   
    {                                                                            
        return $query->has('musicians');                                         
    }                                                                            

Relationship of Event Model:

      public function musicians() : BelongsToMany                                  
      {                                                                            
          return $this->belongsToMany(Contact::class, 'event_musician');           
      }                                                                            
mikebronner commented 6 years ago

@sjbronner This is an issue with scopeWhereInIfNotNull() returning the incorrect type of Builder.