DDC-1357: Queries with multiple joins resulting in multiple scalar results for each top level entity only retain one scalar value for each entity #1700

Closed
opened 2026-01-22 13:22:38 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Sep 1, 2011).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user naderman:

Consider this example:
select g.id, u.id, u.status, count(p.phonenumber) numPhones from Group * g join g.user u join u.phonenumbers p group by g.id, u.status, u.id
With data:

phonenumbers:
    [1, 2, 3, 4, 5, 6]
users:
    [{id: 1, status: developer, phonenumbers: [1, 2]},
     {id: 2, status: developer, phonenumbers: [3]},
     {id: 3, status: developer, phonenumbers: [4, 5, 6]}]
groups:
    [{id: 1, users: [1, 2]]},
     {id:2, users: [3]}]

The result currently is:

array(
    array(
        0 => object(CmsGroup) {
            'id' => 1,
            'users' => Collection(
                object(CmsUser) { 'id' => 1 },
                object(CmsUser) { 'id' => 2 }
            )
         },
        'numPhones' => 1
    ),
    array(
        0 => object(CmsGroup) {
            'id' => 2,
            'users' => Collection(
                object(CmsUser) { 'id' => 3 }
            )
        },
        'numPhones' => 3
    )
)

Note that the first entry contains only one value numPhones => 1, even though there are two users associated with that group. One of whom has 2 phone numbers and the other has 1.

The result I would expect is:

array(
    array(
        0 => object(CmsGroup) {
            'id' => 1,
            'users' => Collection(
                object(CmsUser) { 'id' => 1 },
                object(CmsUser) { 'id' => 2 }
            )
         },
        'numPhones' => array(2, 1)
    ),
    array(
        0 => object(CmsGroup) {
            'id' => 2,
            'users' => Collection(
                object(CmsUser) { 'id' => 3 }
            )
        },
        'numPhones' => array(3)
    )
)

The difference is that numPhones for each row now contains an array of the
scalar values matching the corresponding users.

Originally created by @doctrinebot on GitHub (Sep 1, 2011). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user naderman: Consider this example: `select g.id, u.id, u.status, count(p.phonenumber) numPhones from Group * g join g.user u join u.phonenumbers p group by g.id, u.status, u.id` With data: ``` phonenumbers: [1, 2, 3, 4, 5, 6] users: [{id: 1, status: developer, phonenumbers: [1, 2]}, {id: 2, status: developer, phonenumbers: [3]}, {id: 3, status: developer, phonenumbers: [4, 5, 6]}] groups: [{id: 1, users: [1, 2]]}, {id:2, users: [3]}] ``` The result currently is: ``` array( array( 0 => object(CmsGroup) { 'id' => 1, 'users' => Collection( object(CmsUser) { 'id' => 1 }, object(CmsUser) { 'id' => 2 } ) }, 'numPhones' => 1 ), array( 0 => object(CmsGroup) { 'id' => 2, 'users' => Collection( object(CmsUser) { 'id' => 3 } ) }, 'numPhones' => 3 ) ) ``` Note that the first entry contains only one value numPhones => 1, even though there are two users associated with that group. One of whom has 2 phone numbers and the other has 1. The result I would expect is: ``` array( array( 0 => object(CmsGroup) { 'id' => 1, 'users' => Collection( object(CmsUser) { 'id' => 1 }, object(CmsUser) { 'id' => 2 } ) }, 'numPhones' => array(2, 1) ), array( 0 => object(CmsGroup) { 'id' => 2, 'users' => Collection( object(CmsUser) { 'id' => 3 } ) }, 'numPhones' => array(3) ) ) ``` The difference is that numPhones for each row now contains an array of the scalar values matching the corresponding users.
admin added the ImprovementIncomplete labels 2026-01-22 13:22:38 +01:00
admin closed this issue 2026-01-22 13:22:39 +01:00
Author
Owner

@doctrinebot commented on GitHub (Sep 1, 2011):

Comment created by naderman:

You can find a test case for the correct result here: a1ca3d9847

@doctrinebot commented on GitHub (Sep 1, 2011): Comment created by naderman: You can find a test case for the correct result here: https://github.com/naderman/doctrine2/commit/a1ca3d9847cbc514fc951fb0b221b26fe03a6619
Author
Owner

@malukenho commented on GitHub (Jan 5, 2017):

@Ocramius offered test seems to be wrong and we have similar tests on ObjectHydratorTest#testMixedQueryMultipleDeepMixedFetchJoin

@malukenho commented on GitHub (Jan 5, 2017): @Ocramius offered test seems to be wrong and we have similar tests on `ObjectHydratorTest#testMixedQueryMultipleDeepMixedFetchJoin`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1700