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

[5.7] Wrong bindings when calling whereHas on a relation that uses $withCount #26141

Closed
aocneanu opened this issue Oct 16, 2018 · 0 comments
Closed

Comments

@aocneanu
Copy link
Contributor

aocneanu commented Oct 16, 2018

  • Laravel Version: 5.7.9
  • PHP Version: 7.2.8
  • Database Driver & Version: MySQL 5.7.23

Description:

I have a Project that hasMany Contracts while a Contract belongsTo a project. Contracts can be of many types.

Contract also has a morphMany relation to Document and uses protected $withCount="['documents']

The problem manifests when I try to query Projects that have Contracts of a certain type. In this case nothing is returned because of incorrect bindings.

Steps To Reproduce:

I'm using tinker with DB::enableQueryLog() / DB::getQueryLog()

The following query:

    Project::whereHas('contracts')->get()

returns the collection of projects that have at least one contract, even if the query log shows an extra binding:

     [
       "query" => "select * from `projects` where exists (select * from `contracts` where `projects`.`id` = `contracts`.`project_id`)",
       "bindings" => [
         "App\Contract", //notice this
       ],
       "time" => 1.39,
     ],

While this query:

    Project::whereHas('contracts', function($q) {
        $q->whereType(1);
     })->get()

returns an empty collection and the query log is:

    [
       "query" => "select * from `projects` where exists (select * from `contracts` where `projects`.`id` = `contracts`.`project_id` and `type` = ?)",
       "bindings" => [
         "App\Contract", //again
         1,
       ],
       "time" => 1.39,
     ]

The query as shown by the DebugBar:

select * from `projects` where exists (select * from `contracts` where `projects`.`id` = `contracts`.`project_id` and `type` = 'App\Contract')

I found many previous issues and PRs related to this problem but apparently the above scenario is still not covered:

ref #24240, #24865, #25280, #24240

#25997 will probably make possible at least a workaround

@aocneanu aocneanu changed the title Wrong bindings when calling whereHas on a relation that uses $withCount [5.7]Wrong bindings when calling whereHas on a relation that uses $withCount Oct 16, 2018
@aocneanu aocneanu changed the title [5.7]Wrong bindings when calling whereHas on a relation that uses $withCount [5.7] Wrong bindings when calling whereHas on a relation that uses $withCount Oct 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant