Array hydrator returns null values instead of actual elements in one-to-many relationships #6873

Open
opened 2026-01-22 15:40:28 +01:00 by admin · 0 comments
Owner

Originally created by @LinasRam on GitHub (Nov 11, 2021).

Bug Report

Q A
BC Break no
Version 2.10.2

Summary

The issue is associated with DBMS (MariaDB, in my case) returning same joined rows in different order for separate parent rows. When row ordering is 'random', it may cause ArrayHydrator mapping problems, which results in null values in nested arrays of one-to-many relationships. Check the "Current behavior" section, it will be easier to understand by example.

Current behavior

Assume that we have a result set of these rows:

a__id a__topic u__id u__username p__phonenumber
1 Article 1 1 LinasRam 1234567890
1 Article 1 1 LinasRam 9876543210
2 Article 2 1 LinasRam 9876543210
2 Article 2 1 LinasRam 1234567890

We fetch two articles, which belong to the same user and also join user's phone numbers. The key point is that for second article rows, phone numbers are in different order than they were for the first one.
Array hydrated results of such dataset will return:

[
    [
        'id' => 1,
        'topic' => 'Article 1',
        'user' => [
            'id' => 1,
            'username' => 'LinasRam',
            'phonenumbers' => [
                ['phonenumber' => '1234567890'],
                ['phonenumber' => '9876543210'],
            ],
        ],
    ],
    [
        'id' => 2,
        'topic' => 'Article 2',
        'user' => [
            'id' => 1,
            'username' => 'LinasRam',
            'phonenumbers' => [
                ['phonenumber' => '9876543210'],
                null, // <--- The problem is this null
            ],
        ],
    ],
];

As you can see, in the second article there is a null phone number and it is a bug.

How to reproduce

Added a pull request with a unit test and a possible solution to this bug: #9186

Expected behavior

The expected result of my provided example should look like this:

[
    [
        'id' => 1,
        'topic' => 'Article 1',
        'user' => [
            'id' => 1,
            'username' => 'LinasRam',
            'phonenumbers' => [
                ['phonenumber' => '1234567890'],
                ['phonenumber' => '9876543210'],
            ],
        ],
    ],
    [
        'id' => 2,
        'topic' => 'Article 2',
        'user' => [
            'id' => 1,
            'username' => 'LinasRam',
            'phonenumbers' => [
                ['phonenumber' => '9876543210'],
                ['phonenumber' => '1234567890'],
            ],
        ],
    ],
];
Originally created by @LinasRam on GitHub (Nov 11, 2021). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | 2.10.2 #### Summary The issue is associated with DBMS (MariaDB, in my case) returning same joined rows in different order for separate parent rows. When row ordering is 'random', it may cause `ArrayHydrator` mapping problems, which results in null values in nested arrays of one-to-many relationships. Check the **"Current behavior"** section, it will be easier to understand by example. #### Current behavior Assume that we have a result set of these rows: | a__id | a__topic | u__id | u__username | p__phonenumber | - | - | - | - | - | 1 | Article 1 | 1 | LinasRam | 1234567890 | 1 | Article 1 | 1 | LinasRam | 9876543210 | 2 | Article 2 | 1 | LinasRam | 9876543210 | 2 | Article 2 | 1 | LinasRam | 1234567890 We fetch two articles, which belong to the same user and also join user's phone numbers. The key point is that for second article rows, phone numbers are in different order than they were for the first one. Array hydrated results of such dataset will return: ``` [ [ 'id' => 1, 'topic' => 'Article 1', 'user' => [ 'id' => 1, 'username' => 'LinasRam', 'phonenumbers' => [ ['phonenumber' => '1234567890'], ['phonenumber' => '9876543210'], ], ], ], [ 'id' => 2, 'topic' => 'Article 2', 'user' => [ 'id' => 1, 'username' => 'LinasRam', 'phonenumbers' => [ ['phonenumber' => '9876543210'], null, // <--- The problem is this null ], ], ], ]; ``` As you can see, in the second article there is a `null` phone number and it is a bug. #### How to reproduce Added a pull request with a unit test and a possible solution to this bug: #9186 #### Expected behavior The expected result of my provided example should look like this: ``` [ [ 'id' => 1, 'topic' => 'Article 1', 'user' => [ 'id' => 1, 'username' => 'LinasRam', 'phonenumbers' => [ ['phonenumber' => '1234567890'], ['phonenumber' => '9876543210'], ], ], ], [ 'id' => 2, 'topic' => 'Article 2', 'user' => [ 'id' => 1, 'username' => 'LinasRam', 'phonenumbers' => [ ['phonenumber' => '9876543210'], ['phonenumber' => '1234567890'], ], ], ], ]; ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6873