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

Add join support for tables with PK different than "id" #1077

Merged
merged 19 commits into from
Aug 20, 2023

Conversation

abbadon1334
Copy link
Collaborator

@abbadon1334 abbadon1334 commented Dec 15, 2022

related with #803

@abbadon1334
Copy link
Collaborator Author

abbadon1334 commented Dec 15, 2022

What do you think about the naming and description of this, because that 'idField' is for the FakeModel, not the table itself :

data/src/Model/Join.php

Lines 89 to 93 in 2426c4d

* Custom definition if $idField for of ForeignModel
* By Default ForeignModel idField is equal to 'id'
* To be used when the joined table doesn't have a field named 'id'
*/
public ?string $foreignModelIdField = null;

i decided to make it nullable because in method setDefaults($defaults) nulled values will not be set

src/Model/Join.php Outdated Show resolved Hide resolved
tests/JoinSqlTest.php Outdated Show resolved Hide resolved
tests/JoinSqlTest.php Outdated Show resolved Hide resolved
tests/JoinSqlTest.php Outdated Show resolved Hide resolved
@mvorisek mvorisek changed the title Joining tables on non-id fields Add Join support for tables with PK different than "id" Dec 16, 2022
@mvorisek
Copy link
Member

Does this PR really solve #803? The issue describes something else, this PR adds support for PK to be different than "id". But if PK is "id" and we join based on a different column, the foreign model still needs to have the knowledge about PK to be "id", for AI (autoincrement) to work at least (not mentioning fake model will never execute proper model hooks).

@abbadon1334
Copy link
Collaborator Author

Does this PR really solve #803? The issue describes something else, this PR adds support for PK to be different than "id". But if PK is "id" and we join based on a different column, the foreign model still needs to have the knowledge about PK to be "id", for AI (autoincrement) to work at least (not mentioning fake model will never execute proper model hooks).

You are right about the hooks, it is a FakeModel so the only functionality of this PR is to build a correct query to avoid raising a DSQL Exception when an update on joined fields occurs, but no hook will be triggered.

So join can be done directly with models? ( like this $model->join($joinedModel) )

@mvorisek
Copy link
Member

that is the direction, see #960, however Join class still relies on many hooks as we must hold a reference to joined data and that has limitations currently, imagine like same entity joined/referenced back in one model

@abbadon1334
Copy link
Collaborator Author

that is the direction, see #960, however Join class still relies on many hooks as we must hold a reference to joined data and that has limitations currently, imagine like same entity joined/referenced back in one model

Ok, I will do some tests.

@abbadon1334
Copy link
Collaborator Author

Copy link
Member

@mvorisek mvorisek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The src changes LGTM.

Here is a summary of the discussion above:

#1077 (comment) - @abbadon1334 what usecases does it solve vs. this PR? Some short example?

@mvorisek mvorisek force-pushed the feature/join-foreign-id-field-not-id branch from 7836b65 to 7b4d41c Compare August 12, 2023 17:31
@mvorisek mvorisek force-pushed the feature/join-foreign-id-field-not-id branch 4 times, most recently from e4e47a0 to d3933ca Compare August 20, 2023 01:41
@mvorisek mvorisek force-pushed the feature/join-foreign-id-field-not-id branch from cf347c7 to 6a5817f Compare August 20, 2023 02:19
@mvorisek mvorisek marked this pull request as ready for review August 20, 2023 09:20
@mvorisek mvorisek changed the title Add Join support for tables with PK different than "id" Add join support for tables with PK different than "id" Aug 20, 2023
@mvorisek mvorisek merged commit 2c335f7 into develop Aug 20, 2023
@mvorisek mvorisek deleted the feature/join-foreign-id-field-not-id branch August 20, 2023 09:44
@mvorisek
Copy link
Member

Thank you, @abbadon1334, for this contribution and sorry for letting you wait almost a year. Fixing the DBAL issues needed for proper FK creation - #1117 - was not easy.

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