-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
EntityValueResolver injecting entities when it shouldn't #50739
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
Comments
I haven't gone through all the cases, but if you look at the actual queries it makes sense, e.g. case 2.1: SELECT t0.name AS name_1, t0.id AS id_2, t0.parent_id AS parent_id_3, t0.root_id AS root_id_4
FROM foo t0 WHERE t0.root_id = 1 AND t0.parent_id = 2 LIMIT 1 The doctrine:
orm:
controller_resolver:
# Set to false to disable using route placeholders as lookup criteria when the primary key
# doesn't match the argument name
auto_mapping: false |
Good question, not sure what the answer is. The option is mentioned here, but I knew about it from the original PR, so I can't judge how easy it is to find out about it without having some prior knowledge.
Honestly, I'm having trouble understanding this part, so I can't really comment 😄 . What inconsistencies are you referring to? |
No wonder. I meant to ask you to have a look at cases 4 and 5. Let's blame the early morning and lack of coffee on my end. Let me take you through the key points: Case 4:
Calling The entity resolver finds a result when searching for Case 5:
Calling There is a bit of a twist there too. The So what if the That's why I generated two sets of root-parent-child fixtures. Going back to "Case 4", if you provide a This is a correct result of the current behaviour because both So there you have it. Case 4 populates |
That's odd, I'm getting |
Yes, but not with I think I need to gather my thoughts to re-phrase what's the problem here. I hear your point with the If nothing else, the docs might need an upgrade. I'll come back in a few days to either explain my beef or close this. Thanks for taking time to discuss this topic. |
This issue is the reason why I'm advocating since years to kill this |
Hey, thanks for your report! |
Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3 |
Hey, I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen! |
I played with the reproducer and I confirm all cases are fixed by #54455, provided we set framework.controller_resolver.auto_mapping to false |
@nicolas-grekas Great news, sorry for the no-answer; I was planning to check it out over the weekend. Do you still need me to see what's up? If you're happy, I'm happy. |
… favor of mapped route parameters (nicolas-grekas) This PR was merged into the 7.1 branch. Discussion ---------- [DoctrineBridge] Deprecate auto-mapping of entities in favor of mapped route parameters | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | | Issues | Fix #50739 | License | MIT Auto-mapping of entities on controllers is a foot-gun when multiple entities are listed on the signature of the controller. This is described extensively by e.g. `@stof` in the linked issue and in a few others. The issue is that we try to use all request attributes to call a `findOneBy`, but when there are many entities, there can be an overlap in the naming of the field/associations of both entities. In this PR, I propose to deprecate auto-mapping and to replace it with mapped route parameters, as introduced in #54720. If we go this way, people that use auto-mapping to e.g. load a `$conference` based on its `{slug}` will have to declare the mapping by using `{slug:conference}` instead. That makes everything explicit and keeps a nice DX IMHO, by not forcing a `#[MapEntity]` attribute for simple cases. Commits ------- a49f9ea [DoctrineBridge] Deprecate auto-mapping of entities in favor of mapped route parameters
Found this issue while trying to resolve the deprecation message. Shouldn't this have a recipe? Or does it truly deserve manual configuration?? Symfony 6.4.6 here. Can't upgrade to 7 yet. |
Symfony version(s) affected
6.3.0
Description
When a route parameter name matches a relation name in a Doctrine entity,
EntityValueResovler
is confused without providing theid
parameter, such as#[MapEntity(id: 'parameterName')]
. While providing the ID works, I think the default behaviour, injecting the first found entity, is incorrect.Because this "inject first child" behaviour is inconsistent (sometimes the injected value is
null
, sometimes it's a first entity i n a set), I think this is at least a bug.How to reproduce
I wrote a minimal reproducer with a few scenarios. Check out https://github.com/janklan/map-entity-bug and follow readme to see it.
You should see something along the lines of:
When you click either one of the scenarios (say 2.1), you'll see a
dd()
result of whatever theDefaultController
received:The description next to each scenario should tell you whether I expected the $child to be populated or not, and which one.
Possible Solution
When the resolver has no data to fetch stuff, it should resolve into
null
.Additional Context
This issue was previously discussed in #47166 and attempts to fix it were done in #47242, but I tripped over it again today, when I found an unexpected populated entity.
Some time ago I also reported the same behaviour in SensioFrameworkBundle repo for the now-deprecated ParamConverter, where I believe it was classed as "won't fix" because of the pending deprecation. I can't see the issues anymore, so I can't give you more detail.
The text was updated successfully, but these errors were encountered: