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

[ObjectMapper] merge nested properties when targeting the same class#62511

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
soyuka wants to merge1 commit intosymfony:8.1
base:8.1
Choose a base branch
Loading
fromsoyuka:object-mapper-nested-2

Conversation

@soyuka
Copy link
Contributor

@soyukasoyuka commentedNov 25, 2025
edited
Loading

QA
Branch?8.0
Bug fix?no
New feature?yes
Deprecations?no
IssuesFix#62357
LicenseMIT

Replacement for#62377

documentation## Mapping Nested Objects to the Same Target

When a property of the source object contains another object that maps to the
same target class as the parent, the properties of that nested object are
merged into the current target instance. This is useful for flattening nested
DTO structures into a single resource.

Consider the following example whereBankDataDto contains aBankDto,
and both map toBankDataResource:

// src/Dto/BankDataDto.phpnamespaceApp\Dto;useApp\Api\BankDataResource;useSymfony\Component\ObjectMapper\Attribute\Map;#[Map(target: BankDataResource::class)]class BankDataDto{publicstring$iban;publicBankDto$bank;}// src/Dto/BankDto.phpnamespaceApp\Dto;useApp\Api\BankDataResource;useSymfony\Component\ObjectMapper\Attribute\Map;#[Map(target: BankDataResource::class)]class BankDto{    #[Map(target:'bic')]publicstring$bic;    #[Map(target:'bankCode')]publicstring$code;    #[Map(target:'bankName')]publicstring$name;}// src/Entity/BankDataResource.phpnamespaceApp\Api;class BankDataResource{publicstring$iban;publicstring$bic;publicstring$bankCode;publicstring$bankName;}

When mappingBankDataDto, the mapper will automatically process the$bank
property and map its fields (bic,code,name) to the same
BankDataResource instance created for the parent:

$bankDto =newBankDto();$bankDto->bic ='BIC123';$bankDto->code ='BANK001';$bankDto->name ='Test Bank';$dto =newBankDataDto();$dto->iban ='IBAN12345';$dto->bank =$bankDto;$mapper =newObjectMapper();$resource =$mapper->map($dto, BankDataResource::class);// $resource->iban === 'IBAN12345'// $resource->bic === 'BIC123' (mapped from the nested BankDto)// $resource->bankCode === 'BANK001'// $resource->bankName === 'Test Bank'

@carsonbotcarsonbot added this to the7.4 milestoneNov 25, 2025
@soyukasoyuka changed the base branch from7.4 to8.0November 26, 2025 08:56
@soyukasoyukaforce-pushed theobject-mapper-nested-2 branch 2 times, most recently from71a147f to027c81dCompareNovember 26, 2025 08:58
@soyuka
Copy link
ContributorAuthor

I'm not a huge fan of this DX but it's the best I could come up with,@temp@daFish could I have your opinion?

@OskarStark
Copy link
Contributor

This must now target 8.1 please

@OskarStarkOskarStark modified the milestones:7.4,8.1Nov 29, 2025
@soyukasoyuka changed the base branch from8.0 to8.1December 1, 2025 07:24
@soyukasoyukaforce-pushed theobject-mapper-nested-2 branch from027c81d toc0a2426CompareDecember 1, 2025 07:25
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@welcoMatticwelcoMatticAwaiting requested review from welcoMattic

@chalasrchalasrAwaiting requested review from chalasr

@OskarStarkOskarStarkAwaiting requested review from OskarStark

Assignees

No one assigned

Projects

None yet

Milestone

8.1

Development

Successfully merging this pull request may close these issues.

3 participants

@soyuka@OskarStark@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp