Fetching entities with Composite Key Relations and null values #11486
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Recreation of #8425 as it was closed but never actually resolved and the issues still persist.
Description from the previous PR (Background):
In our database design we are using entities with composite keys, and we are using composite keys relations between entities.
Let say we have the following entities with Primary Keys (PK):
Company
(PK:company_code
)Customer
(PK composite:company_code
+customer_code
)Invoice
(PK composite:company_code
+invoice_code
)and the following relations in
Invoice
:Company
(usingcompany_code
), not nullableCustomer
(using composite keycompany_code
+customer_code
), nullableNow we have
company_code
(not null) but nocustomer_code
(null).We want to fetch
Invoice
entity, so that we get not nullCompany
, and nullCustomer
.The problem is that loading
Invoice
with nullCustomer
is not working - we are getting the following exception:This PR provide a fix, to load the
Customer
in the described case.The change is pretty simply, and no other tests are affected.
NOTE: Loading
Company
is not a problem as it is using relation on just one column (not a composite key). The description includes it just for completeness of the example.Workaround
We have discovered that we could use one of these workarounds - but both are not perfect:
Solution
The solution proposed in this PR is very minimal - just removal od one of the condition, which seems to be not covered, and I couldn't find track why it has been even added there. Based on the example provided in this PR, the removed condition seems to be invalid.
PR contains two commits:
Additional considerations
The following comment confused me a lot:
orm/src/UnitOfWork.php
Line 2456 in 9d4f54b
and I did additional investigation if the proposed solution here is correct.
In the below we are removing just first part of the condition:
orm/src/UnitOfWork.php
Lines 2470 to 2476 in 9d4f54b
and I was wonder if the target entity could/should know if it is a part of a FK somewhere else - and I can't even see the reason behind that, so I believe this is the correct fix as it is the case when the filed "is part of target's entity primary key".
Hope it all makes sense and we can get it in. Thanks 🙌