-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Iterate with fetch join in subquery #5868
Comments
This happens when using either MANY_TO_MANY or ONE_TO_MANY join in your query then you cannot iterate over it because it is potentially possible that the same entity could be in multiple rows. If you add a distinct to your query then all will work as it will guarantee each record is unique. $qb = $this->createQueryBuilder('o');
$qb->distinct()->join('o.manyRelationship');
$i = $qb->iterator;
echo 'Profit!'; |
I faced a similar problem.
DQL
That's right. The So, this condition is not entirely correct and maybe it's a bug. if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) === true &&
(! $this->query->getHint(self::HINT_DISTINCT) || isset($this->selectedClasses[$joinedDqlAlias]))) {
if ($association instanceof ToManyAssociationMetadata) {
throw QueryException::iterateWithFetchJoinNotAllowed($owningAssociation);
}
} |
Anower example
|
I do not believe your explanation as to why a distinct is pointless is correct. A |
@maxolasersquad Yes. Sorry. I forgot to add a grouping. The second example demonstrates the problem. With and without |
I wonder if you remove the group by and just leave in the distinct if that would work. |
Sorry. I grouped the results by the wrong field. This query returns 37 records for my data (wrong result), but it breaks when trying to iterate.
This query returns 37 records for my data.
This query also returns 37 records for my data, but it breaks when trying to iterate according to the query results.
Adding a
If i group by action id, then returns 4 records for my data. This is the correct result. This query does not result in error.
The addition of
This query will return 37 records for my data (wrong result) and lead to error
As in the previous example, adding a
Adding a group will returns 4 records and will not result in error in
This query will return the correct data, but will also lead to exception in
From my experiments, i conclude that this condition does not work correctly. It is wrong to demand the use of |
Hi. I ran into the same error with a query with a leftJoin on a one-to-many relation but with groupBy on root entity id, ensuring each entity is returned only once. I'm guessing Doctrine is able to detect join on one-to-many but maybe it's too complicated to assert a groupBy ensure entities unicity in selected rows. I fixed this by adding $queryBuilder = $this
->createQueryBuilder()
// ...
->select("stuff.id, GROUP_CONCAT(stuff.name separator ', ')")
->leftJoin('foobar.stuffs', 'stuff')
->groupBy('foobar.id');
return $queryBuilder
->getQuery()
->setHint(SqlWalker::HINT_DISTINCT, true)
->toIterable(); |
There are problem with query->iterate() method when using subquery with fetch join. E.g. there are a User and Group classes, User has a collection of group. When using query like SELECT u from User u WHERE u.id IN (SELECT DISTINCT u2.id from User u2 join u2.groups g WHERE g.name='Admin')
there are exception using iterate() method: Iterate with fetch join in class User using association groups not allowed.
But this query still return one row per User object, and there should be no problems with hydrating.
It's possible to iterate using such queries?
The text was updated successfully, but these errors were encountered: