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

issue with withCount method #21464

Closed
wanghanlin opened this issue Sep 29, 2017 · 4 comments
Closed

issue with withCount method #21464

wanghanlin opened this issue Sep 29, 2017 · 4 comments

Comments

@wanghanlin
Copy link
Contributor

wanghanlin commented Sep 29, 2017

  • Laravel Version: 5.*
  • PHP Version: not related
  • Database Driver & Version: not related

Description:

There is an issue with withCount since it re-assign Query Builder columns property by select() but didn't clean select bindings, it cause the final SQL binding mismatch.

Steps To Reproduce:

This issue is originally found in here

As a note:

  1. there is one SQL select "bookings".*, (select count(*) from "extras" where "bookings"."id" = "extras"."booking_id" and "app_id" = ?) as "extras_count" from "bookings" , and after that, this same builder instance changed to select count(*) as "booking_count" from "bookings" but binding is kept.
  2. the final SQL have this binding: [1,"open"] but it only have one ? in SQL, so it replace at wrong place.

Fix

See #21465

Extra

during the debugging, i found getRelationExistenceCountQuery() and getRelationExistenceQuery() in Illuminate\Database\Eloquent\Relations\Relation class are not using $parentQuery any more, should we remove this parameter in 5.6?

@themsaid
Copy link
Member

Please share enough steps of reproduction, how can I reproduce this faulty query using Eloquent code?

@wanghanlin
Copy link
Contributor Author

wanghanlin commented Sep 29, 2017

@themsaid Hi, the steps to reproduce is very clear in that post, basically need to have 3 models, first model has many second model, second model has many third model. Second model have protected property $withCount of third model. Third model have a global scope. Using first model Model::withCount method with a function have where query will cause the problem. You can reproduce the error easily just using the code in that post. Sorry about the format, I'm typing from mobile. If something is not clear I can post a more detailed are by step post later on my computer

@themsaid
Copy link
Member

Check #21468

@wanghanlin
Copy link
Contributor Author

i think that doesn't fix fully, I opened another issue #21472 for more explanation

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