Allow ResultSetMapping to hydrate unmapped entity parameter #7394

Open
opened 2026-01-22 15:51:11 +01:00 by admin · 2 comments
Owner

Originally created by @Jarod-MIDY on GitHub (Jul 10, 2024).

Feature Request

Q A
New Feature yes
RFC yes
BC Break not sure

Summary

While using the ResultSetMapping object to hydrate a doctrine-managed entity from a native SQL request I could not hydrate parameters that do not correspond to a db column.

the SQL query counts the total number of pages by book. I don't want to add a nb_pages column in my book table.

the entity :

#[ORM\Entity(repositoryClass: BookRepository::class)]
class Book {

    #[ORM\Column(length: 255)]
    public string $title = ''; // this I can hydrate as expected

    public ?int $nbPages = null; // this doesn't seem to be hydratable with ResultSetMapping
   [...]
}

the mapping :

        $rsm = new ResultSetMapping();
        $rsm->addEntityResult(Book::class, 'project')
            ->addMetaResult('project', 'project_id', 'id', true)
            ->addFieldResult('project', 'project_id', 'id')
            ->addFieldResult('project', 'project_created_at', 'createdAt')
            ->addFieldResult('project', 'project_updated_at', 'updatedAt')
            ->addFieldResult('project', 'project_title', 'title');
        $rsm->addJoinedEntityResult(Chapter::class, 'c', 'project', 'chapters')
            ->addMetaResult('c', 'chapter_id', 'id', true)
            ->addFieldResult('c', 'chapter_id', 'id')
            ->addFieldResult('c', 'chapter_title', 'title')
            ->addFieldResult('c', 'chapter_position', 'position')
            ->addFieldResult('c', 'chapter_created_at', 'createdAt')
            ->addFieldResult('c', 'chapter_updated_at', 'updatedAt');
        $rsm->addJoinedEntityResult(Page::class, 'p', 'c', 'pages')
            ->addFieldResult('p', 'page_id', 'id')
            ->addFieldResult('p', 'page_created_at', 'createdAt')
            ->addFieldResult('p', 'page_updated_at', 'updatedAt')
            ->addFieldResult('p', 'page_position', 'position')
            ->addFieldResult('p', 'page_published_at', 'publishedAt')
            ->addFieldResult('p', 'page_title', 'title');

// I tried to hydrate it as if It were a DTO as explained in the documentation but this did not work :
        $rms->addScalarResult('total_pages', 'nbPages');
        $rsm->newObjectMappings['nbPages']  = [
           'className' => Book::class,
           'objIndex'  => 0,
           'argIndex' => 'nbPages'
        ];
Originally created by @Jarod-MIDY on GitHub (Jul 10, 2024). ### Feature Request <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | New Feature | yes | RFC | yes | BC Break | not sure #### Summary <!-- Provide a summary of the feature you would like to see implemented. --> While using the ResultSetMapping object to hydrate a doctrine-managed entity from a native SQL request I could not hydrate parameters that do not correspond to a db column. the SQL query counts the total number of pages by book. I don't want to add a nb_pages column in my book table. the entity : ```php #[ORM\Entity(repositoryClass: BookRepository::class)] class Book { #[ORM\Column(length: 255)] public string $title = ''; // this I can hydrate as expected public ?int $nbPages = null; // this doesn't seem to be hydratable with ResultSetMapping [...] } ``` the mapping : ```php $rsm = new ResultSetMapping(); $rsm->addEntityResult(Book::class, 'project') ->addMetaResult('project', 'project_id', 'id', true) ->addFieldResult('project', 'project_id', 'id') ->addFieldResult('project', 'project_created_at', 'createdAt') ->addFieldResult('project', 'project_updated_at', 'updatedAt') ->addFieldResult('project', 'project_title', 'title'); $rsm->addJoinedEntityResult(Chapter::class, 'c', 'project', 'chapters') ->addMetaResult('c', 'chapter_id', 'id', true) ->addFieldResult('c', 'chapter_id', 'id') ->addFieldResult('c', 'chapter_title', 'title') ->addFieldResult('c', 'chapter_position', 'position') ->addFieldResult('c', 'chapter_created_at', 'createdAt') ->addFieldResult('c', 'chapter_updated_at', 'updatedAt'); $rsm->addJoinedEntityResult(Page::class, 'p', 'c', 'pages') ->addFieldResult('p', 'page_id', 'id') ->addFieldResult('p', 'page_created_at', 'createdAt') ->addFieldResult('p', 'page_updated_at', 'updatedAt') ->addFieldResult('p', 'page_position', 'position') ->addFieldResult('p', 'page_published_at', 'publishedAt') ->addFieldResult('p', 'page_title', 'title'); // I tried to hydrate it as if It were a DTO as explained in the documentation but this did not work : $rms->addScalarResult('total_pages', 'nbPages'); $rsm->newObjectMappings['nbPages'] = [ 'className' => Book::class, 'objIndex' => 0, 'argIndex' => 'nbPages' ]; ```
Author
Owner

@soullivaneuh commented on GitHub (Jul 11, 2024):

The logic described above produce the following error:

ErrorException:
Warning: Undefined array key "nbPages"

  at vendor/doctrine/orm/src/Internal/Hydration/AbstractHydrator.php:549
  at Doctrine\ORM\Internal\Hydration\AbstractHydrator->hydrateColumnInfo('total_pages')
     (vendor/doctrine/orm/src/Internal/Hydration/AbstractHydrator.php:414)

It is because the nbPages is not part of the class metadata used by AbstractHydrator.

The ColumnResult annotation looks to be the solution in order to make it considering the field to the metadata as a result only property.

However, this annotation was removed before the attribute migration by https://github.com/doctrine/orm/pull/7095 and https://github.com/doctrine/orm/pull/10114 without any explanation of why referenced.

@soullivaneuh commented on GitHub (Jul 11, 2024): The logic described above produce the following error: ``` ErrorException: Warning: Undefined array key "nbPages" at vendor/doctrine/orm/src/Internal/Hydration/AbstractHydrator.php:549 at Doctrine\ORM\Internal\Hydration\AbstractHydrator->hydrateColumnInfo('total_pages') (vendor/doctrine/orm/src/Internal/Hydration/AbstractHydrator.php:414) ``` It is because the `nbPages` is not part of the class metadata used by `AbstractHydrator`. The [`ColumnResult`](https://www.doctrine-project.org/projects/doctrine-orm/en/2.16/reference/annotations-reference.html#columnresult) annotation looks to be the solution in order to make it considering the field to the metadata as a result only property. However, this annotation was removed before the attribute migration by https://github.com/doctrine/orm/pull/7095 and https://github.com/doctrine/orm/pull/10114 without any explanation of why referenced.
Author
Owner

@mkoskl commented on GitHub (Jan 5, 2026):

I have similar use-case. Is there a solution for this?

@mkoskl commented on GitHub (Jan 5, 2026): I have similar use-case. Is there a solution for this?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7394