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.4] add a "second local key" to HasManyThrough #18997

Conversation

phroggyy
Copy link
Contributor

@phroggyy phroggyy commented Apr 29, 2017

Let's take the example in the docs, with countries, users, and posts. With the current implementation of HasManyThrough, we can specify the local key to use on countries (id), the foreign key on users (country_id), and the foreign key on the target model (user_id on posts). However, we can't specify the local key on the intermediary table. Consider the following scenario, where we keep a separate table for stripe accounts:

users:
id | email | password

billing_accounts:
id | user_id | stripe_customer_id | name | address_line_1 | address_line_2 | city | zip | country [`user_id` references `id` on `users`]

transactions:
id | customer_id | charge_id | amount | currency | status [`customer_id` references `stripe_customer_id` on `billing_accounts`]

In this case, if we wish to retrieve a user's transactions, we can't, because we can't specify that the key to join billing_accounts and transactions on should be billing_accounts.stripe_customer_id = transactions.customer_id. Instead, Laravel assumes that we wish to use the primary key of our intermediary, something that can limit users in queries like this.

@phroggyy phroggyy force-pushed the 5.4-feat/has-many-through-second-local-key branch 3 times, most recently from 63283ed to 80f144b Compare April 29, 2017 13:20
@phroggyy phroggyy changed the title [WIP][5.4] add a "second local key" to HasManyThrough [5.4] add a "second local key" to HasManyThrough Apr 29, 2017
@taylorotwell
Copy link
Member

Let's get rid off those over-mocked tests and just write a simple integration test for this relationship type. You can create a new test file for it.

@phroggyy phroggyy force-pushed the 5.4-feat/has-many-through-second-local-key branch 4 times, most recently from df88af4 to f1cee1f Compare May 2, 2017 21:50
@phroggyy phroggyy force-pushed the 5.4-feat/has-many-through-second-local-key branch from f1cee1f to 798dad4 Compare May 2, 2017 21:51
@phroggyy
Copy link
Contributor Author

phroggyy commented May 2, 2017

@taylorotwell done! I've recreated every test that was in the old unit test class too

@ludo237
Copy link

ludo237 commented May 3, 2017

This change could create more flexible relationships and database schemas👍

@phroggyy
Copy link
Contributor Author

phroggyy commented May 5, 2017

@taylorotwell any update on this?

@taylorotwell
Copy link
Member

I wonder if there is a better variable name than secondLocalKey. Maybe something more descriptive of what it is.

@phroggyy
Copy link
Contributor Author

phroggyy commented May 5, 2017

@taylorotwell my other suggestion would be intermediateLocalKey. That was my first thought, but the reasoning behind secondLocalKey is that $localKey matches with $firstKey and $secondLocalKey matches with $secondKey (in terms of what you join on). I thought that might be less mentally straining, but I'm not sure.

Another option would be $throughLocalKey

@phroggyy
Copy link
Contributor Author

phroggyy commented May 5, 2017

Once this is merged, I'll also PR to docs to clarify regarding the keys, I think they could use it anyway.

@taylorotwell
Copy link
Member

@phroggyy i think this looks good but I need you to send it to master branch since it changes a method signature.

@phroggyy phroggyy deleted the 5.4-feat/has-many-through-second-local-key branch May 9, 2017 09:07
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.

4 participants