Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to use ObjectMapper to map nested array members#60994

Unanswered
imbachb asked this question inQ&A
Discussion options

I would like to use the new ObjectMapper (https://symfony.com/doc/current/object_mapper.html) to map an Entity that has an array of another entity as its members to a Dto.
Following is an example of the Entity and the Dto.

// Entity#[ORM\Entity]class ExerciseResult{/**     * @var Collection<int, CoordinationComponentResult>     */    #[ORM\OneToMany(mappedBy:'exerciseResult', targetEntity: CoordinationComponentResult::class)]privateCollection$koordinationComponentResults;}// Dtos#[Map(source: ExerciseResult::class)]class ExerciseResultDto{/** @var CoordinationComponentResultDto[] */    #[Map(source:'coordinationComponentResults.toArray')]publicarray$coordinationComponentResults;// ...}#[Map(source: CoordinationComponentResult::class)]class CoordinationComponentResultDto{publicint$id;publicint$reachedScore;publicfloat$accuracy;}

When mapping the Entity to its Dto

$mapper =newObjectMapper(propertyAccessor: PropertyAccess::createPropertyAccessor());$exerciseResultDto =newExerciseResultDto();$mapper->map($exerciseResult,$exerciseResultDto);

the resulting$exerciseResultDto's member isCoordinationComponentResult[] instead ofCoordinationComponentResultDto[].
I'd assume the solution would look something like

 #[Map(source:'coordinationComponentResults.toArray', target: CoordinationComponentResultDto::class .'[]' )]public array$coordinationComponentResults;

But this does not work either.
How can one map a nested array members to their Dto counterpart?

You must be logged in to vote

Replies: 0 comments

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
1 participant
@imbachb

[8]ページ先頭

©2009-2025 Movatter.jp