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

[8.x] Eager loading MorphTo relationship does not honor each models $keyType #34531

Merged
merged 3 commits into from
Sep 24, 2020
Merged

[8.x] Eager loading MorphTo relationship does not honor each models $keyType #34531

merged 3 commits into from
Sep 24, 2020

Conversation

jimpeters
Copy link
Contributor

@jimpeters jimpeters commented Sep 24, 2020

When eager loading a morphTo relationship that could contain models using a $keyType of 'string' each query that is built is not making sure the bound parameter is cast to string due to the dictionary being built using each model's ID as an array key.

Table A has morphTo "owner" relationship.

Table B is a related model that has a $keyType of string. Even though some of its values can be "numeric" at times.

Entries in table A for B include id values of [1,2,3]

When building the whereIn query for the database if you are not casting to a string MySQL will not use the index for that query when the column type is a char requiring a full table scan of Table B.

I really want to write tests for this but am not smart enough figure out a refactor that would give me the raw query before it's executed in getResultsByType($type) or even mock the query builder to catch the whereIn() call and examine the parameters passed in.

Thanks for your consideration.

@jimpeters jimpeters changed the title Eager loading MorphTo relationship does not honor each models $keyType [8.x] Eager loading MorphTo relationship does not honor each models $keyType Sep 24, 2020
@taylorotwell
Copy link
Member

Is this fixing this issue or a separate issue: #34331

@jimpeters
Copy link
Contributor Author

This is fixing an issue I have with MySQL ignoring index usage when bound parameters are passed as ints when they are numeric due to PHP array behavior of not letting strings that are valid ints be array keys as strings.

I do not have access to SQL Server but after reading his issue I believe this would solve it as well. He is welcome to test this on his code base first as it is a simple change.

I'm sorry I did not open an issue first. Would you like me to or we can use that issue as it's the same answer I believe.

@taylorotwell taylorotwell merged commit 5813b00 into laravel:8.x Sep 24, 2020
@jimpeters jimpeters deleted the morphto-string-casting branch September 24, 2020 20:43
divine added a commit to mongodb/laravel-mongodb that referenced this pull request Sep 30, 2020
softdevee added a commit to softdevee/laravel-mongodb that referenced this pull request Sep 5, 2022
lisadeloach63 added a commit to lisadeloach63/mongodb-laravel that referenced this pull request Oct 7, 2022
PermitinYury pushed a commit to PermitinYury/laravel-mongodb that referenced this pull request Feb 17, 2023
KarenEtheridg added a commit to KarenEtheridg/laravel-mongodb that referenced this pull request Feb 17, 2023
mnphpexpert added a commit to mnphpexpert/laravel-mongodb that referenced this pull request Sep 2, 2024
CDouglas1029 added a commit to CDouglas1029/laravel_MongoDB that referenced this pull request Nov 15, 2024
dev-arrow added a commit to dev-arrow/laravel-mongodb that referenced this pull request Nov 26, 2024
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

Successfully merging this pull request may close these issues.

2 participants