orm icon indicating copy to clipboard operation
orm copied to clipboard

Failure when selecting a NEW DTO with only DTO arguments

Open va5ja opened this issue 10 months ago • 0 comments

Bug Report

Q A
Version 3.3.2

Summary

A simple query builder or DQL statement like the following:

return $this->createQueryBuilder('t')
    ->select(
        'NEW App\Dto\MainDto(
            NEW App\Dto\NestedDto(t.field),
            NEW App\Dto\NestedDto(t.field)
        )'
    )
    ->getQuery()
    ->getResult();

fails with error:

Warning: Undefined array key "class"

If we change the first argument to let's say a string, so:

return $this->createQueryBuilder('t')
    ->select(
        'NEW App\Dto\MainDto(
            t.field,
            NEW App\Dto\NestedDto(t.field),
            NEW App\Dto\NestedDto(t.field)
        )'
    )
    ->getQuery()
    ->getResult();

then the query works.

Current behavior

Having a select with a single NEW DTO with only DTO arguments fails.

Expected behavior

The query works fine.

How to reproduce

It has something to do with ObjectHydrator::hydrateRowData() and more specifically AbstractHydrator::gatherRowData() at the following part:

        foreach ($this->resultSetMapping()->nestedNewObjectArguments as $objIndex => ['ownerIndex' => $ownerIndex, 'argIndex' => $argIndex]) {
            if (! isset($rowData['newObjects'][$ownerIndex . ':' . $argIndex])) {
                continue;
            }

            $newObject = $rowData['newObjects'][$ownerIndex . ':' . $argIndex];
            unset($rowData['newObjects'][$ownerIndex . ':' . $argIndex]);

            $obj = $newObject['class']->newInstanceArgs($newObject['args']);

            $rowData['newObjects'][$ownerIndex]['args'][$argIndex] = $obj;
        }

        foreach ($rowData['newObjects'] as $objIndex => $newObject) {
            $obj = $newObject['class']->newInstanceArgs($newObject['args']);

            $rowData['newObjects'][$objIndex]['obj'] = $obj;
        }

The last $newObject['class'] doesn't exist.

It could be that this is fixed in https://github.com/doctrine/orm/pull/11825

va5ja avatar Mar 05 '25 14:03 va5ja