mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Query with OneToOne JOIN returns single result? #6665
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @dVVIIb on GitHub (Mar 26, 2021).
I have a query of the form:
$query = $entityManager->createQuery('SELECT a.id FROM App\Entities\EntityA a JOIN a.entityStatus s');When the mapping is of the form:
@ORM\OneToOne(targetEntity="App\Entities\EntityStatus", mappedBy="entityA")I get a single result.
However, when the association is OneToMany, I get multiple results for the same data. The only alteration being
@ORM\OneToOne -> @ORM\OneToManyThe data seem to be ok, and map on-to-one to distinct ids.
Am I missing something?
I expect a distinct row per each on-to-one mapping.
"doctrine/doctrine-bundle": "^2.2", "doctrine/doctrine-migrations-bundle": "^3.0", "doctrine/orm": "^2.8"@greg0ire commented on GitHub (Mar 27, 2021):
You are selecting an
id, and I don't think there is a way for the ORM to let you obtain the status entities for id 42 from id 42. Try selecting just a instead? That way the ORM will return a collection ofEntityAobjects.Also, we try to keep Github issues for bug reports and feature requests only. If you have a question, please ask it on Stack Overflow or in one of the channels mentioned at https://www.doctrine-project.org/community/index.html
@dVVIIb commented on GitHub (Mar 27, 2021):
greg0ire, Your answer doesn't make any sense. I did post this here because I think this is a bug. I don't understand why you closed my issue without allowing me to respond. StackOverflow is no place for bug reports. And I tried as you suggested and selected a but this did not help. With the JOIN, I still get a single result. Note, that I don't wish to select a. The association IS identified/enforced by the id. Note that the query I provided is a simplified version reduced to demo the issue. The original query had more fields. If this IS a bug, then this issue should remain open. Otherwise, kindly explain why this is not a bug, and how this issue should be addressed...
@greg0ire commented on GitHub (Mar 27, 2021):
Watch your tone please.
You just responded, and are very much allowed to do so.
Please give more details, I'm not sure I understood what your issue is: sometimes you complain about having too many results: "I get multiple results for the same data", and now you are complaining about getting a single result: "I still get a single result". It would help if you showed a sample of what you have in your tables, and what output you expect from the DQL query above.
Yes, I will reopen if you convince me that it is a bug, but right now, you have not quoted any part of the docs showing that it is one.
@dVVIIb commented on GitHub (Mar 27, 2021):
Sorry, no tone was intended. English is not my first language. Be mindful and considerate of such.
I was previously under the impression that once closed, an issue becomes less noticeable to those other than who are already subscribed. Correct me if I'm wrong.
I think you're confusing me with someone else. I never made any such complaint previously, anywhere. I'm only explaining the issue that I have encountered, which is that a single result is returned, when multiple were expected. Like I mentioned, I have multiple distinct values in my tables, not duplicates. Let me try to show a sample set of data:
The additional columns below are probably irrelevant, but I'm including them for sake of completeness.
Table: EntityA
Id PId AId CTId1 1 3 14 4 3 15 5 3 16 6 3 1Table: entityStatus
Id CId CreatedAt UpdatedAt CreatedBy UpdatedBy CSId1 4 '2021-03-25 14:41:21' '2021-03-25 14:41:21' 5 5 12 5 '2021-03-25 14:44:47' '2021-03-25 14:44:47' 5 5 13 6 '2021-03-25 14:48:30' '2021-03-25 14:48:30' 5 5 1Here, CId maps to Id of EntityA. I have specified the correct columns using @ORM\JoinColumn. To simplify the issue, I'm only using a single JOIN (The original query had more). Once joined, the output should be as follows, if selecting EntityA.Id, entityStatus.Id:
4 15 26 3However, the join only returns a single row.
1 1Is this the expected behavior? If so, how should I get my intended behavior?
And again, no tone is implied or intended. I'm just trying to explain my issue. If you need more info, just ask, and I'll try to clarify...
@greg0ire commented on GitHub (Mar 27, 2021):
It does become less noticeable, but everyone watching this repository still gets a notification every time we post something here.
You can make sure that is indeed correct by running https://github.com/doctrine/orm/blob/2.8.x/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php. If you use Symfony, that would be
bin/console doctrine:schema:validateSo, it looks as if the join is in fact not performed on the correct columns right? It's as if
entityStatus.Idwas used instead ofentityStatus.CId… what is the SQL query being performed? Do you know how to check that?Also, don't worry about your english, it's perfect, especially in your last message.
@dVVIIb commented on GitHub (Mar 27, 2021):
Ah, I see now. Thanks for the clarification. It looks like referencedColumnName in JoinColumn has to be specified. The default didn't work for my schema (I had already specified the column on the opposite side, but missed this one.). Dumping the SQL using $queryBuilder->getSQL() helped. Sorry for posting a false issue, but without your comments, the issue would have been a bit difficult to diagnose. I expect everything will work fine now, and maybe this issue will help someone with a similar issue.
Thank you.
@greg0ire commented on GitHub (Mar 27, 2021):
You're saying this wasn't a bug after all? What a surprise!
Seriously, there were signs that it was not going to be a bug:
doctrine/ormis more than 10 years old. Big bugs have already been caught a long time ago… I know that it's easier for you to get answers from maintainers directly, but can you imagine if everybody did that?Next time, when in doubt, please please go to Stack Overflow. 🙏 Stack Overflow is not super forgiving, but it will teach you to write clear questions, with examples from the start, and maybe you will even get reputation points in the process. And the people that want to help you, that want to offer support, they are there (or on Slack). Here, what we try to do is take care of actual, legitimate bugs.
That's not wrong, but you know what? That works great with Stack Overflow too.