Skip to content
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

Allow ResultSetMapping to hydrate unmapped entity parameter #11548

Open
Jarod-MIDY opened this issue Jul 10, 2024 · 1 comment
Open

Allow ResultSetMapping to hydrate unmapped entity parameter #11548

Jarod-MIDY opened this issue Jul 10, 2024 · 1 comment

Comments

@Jarod-MIDY
Copy link

Jarod-MIDY commented 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'
        ];
@soullivaneuh
Copy link

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 #7095 and #10114 without any explanation of why referenced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants