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

Raw query breaks with Laravel 5.7.14+ #195

Closed
drbyte opened this issue Nov 27, 2018 · 4 comments
Closed

Raw query breaks with Laravel 5.7.14+ #195

drbyte opened this issue Nov 27, 2018 · 4 comments
Assignees

Comments

@drbyte
Copy link
Contributor

drbyte commented Nov 27, 2018

Describe the bug
ErrorException: Undefined index: operator appears when it tries to parse select queries,
presumably because of the new types added in 5.7.14 (InRaw, NotInRaw, IntegerInRaw, IntegerNotInRaw) in the name of improving eager-loading performance.
Ref: laravel/framework@v5.7.13...v5.7.14
Related Laravel PRs from changelog: (#26434, #26453, 3992140, #26471, a3738cf, #26531)

Eloquent Query

        return WeekendAssignments::select('weekend_assignments.*', 'weekend_roles.RoleName', 'weekend_roles.sortorder')
            ->join('weekend_roles', 'weekend_assignments.roleID', '=', 'weekend_roles.id')
            ->where('weekendID', $this->id)
            ->where('confirmed', true)
            ->withoutGlobalScope('visibleWeekendsOnly')
            ->orderBy('weekend_roles.sortorder', 'asc')
            ->orderBy('weekend_roles.id', 'asc')
            ->get();

which generates:

select "weekend_assignments".*, "weekend_roles"."RoleName", "weekend_roles"."sortorder" 
from "weekend_assignments" 
   inner join "weekend_roles" on "weekend_assignments"."roleID" = "weekend_roles"."id" 
where "weekendID" = ? and "confirmed" = ? 
order by "weekend_roles"."sortorder" asc, "weekend_roles"."id" asc

where WeekendAssignments is a basic Eloquent model with the following:

class WeekendAssignments extends Model
{
    protected $with = 'role';

    public function getRouteKeyName()
    {
        return 'weekendID';
    }
    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('visibleWeekendsOnly', function (Builder $builder) {
            $builder->whereIn(
                'weekendID',
                Weekend::where('visibility_flag', '>=', WeekendVisibleTo::Community)->get()->pluck('id')
            );
        });
    }

    //
    public function role()
    {
        return $this->belongsTo(WeekendRoles::class, 'roleID');
    }
    //
}

and WeekendRoles (note: "Roles" here refers to "a position held on a team, such as 'food server'" and not related to app permissions):

class WeekendRoles extends Model
{
    use GeneaLabs\LaravelModelCaching\Traits\Cachable;

    protected $with = 'section';

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('rolesBySortOrder', function (Builder $query) {
            $query->orderBy('sortorder', 'asc');
        });
    }

    //

    public function section()
    {
        // Section is a simple basic Eloquent model
        return $this->belongsTo(Section::class, 'section_id');
    }
}

Stack Trace
/SITE/vendor/genealabs/laravel-model-caching/src/CacheKey.php:102
/SITE/vendor/genealabs/laravel-model-caching/src/CacheKey.php:262
/SITE/vendor/genealabs/laravel-model-caching/src/CacheKey.php:167
/SITE/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1388
/SITE/vendor/genealabs/laravel-model-caching/src/CacheKey.php:170
/SITE/vendor/genealabs/laravel-model-caching/src/CacheKey.php:36
/SITE/vendor/genealabs/laravel-model-caching/src/Traits/Caching.php:76
/SITE/vendor/genealabs/laravel-model-caching/src/CachedBuilder.php:86
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php:155
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php:144
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:564
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:533
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:501
/SITE/app/Weekend.php:405
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:444
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:344
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:317
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1513
/SITE/app/Weekend.php:416
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:444
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:344
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:317
/SITE/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1513
/SITE/app/Http/Controllers/WeekendController.php:94
/SITE/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
/SITE/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45
/SITE/vendor/laravel/framework/src/Illuminate/Routing/Route.php:212
... (usual bootstrap stuff)
/SITE/tests/Feature/WeekendStatusTest.php:135

Environment

  • PHP: 7.2.12
  • Laravel: 5.7.14+
  • Model Caching: 0.3.3

Additional context
In feature tests, had to add withoutExceptionHandling() to see the Exceptions being thrown.

@mikebronner
Copy link
Owner

@drbyte Thanks for the detailed report. I will look into this soon. Your report helps a lot.

@mikebronner
Copy link
Owner

@drbyte Would you be able to help me out a bit? I'm trying to come up with queries that would trigger each of the new query types. Any help you could provide in coming up with different queries to test each of the types would be helpful. :)

@drbyte
Copy link
Contributor Author

drbyte commented Nov 28, 2018

Apologies - I was AFK when you were working on this.

@mikebronner
Copy link
Owner

@drbyte no worries ... I think it's all resolved :)

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

2 participants