Skip to content

Conversation

@soyuka
Copy link
Contributor

@soyuka soyuka commented Nov 25, 2025

Q A
Branch? 8.0
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #62357
License MIT

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 where BankDataDto contains a BankDto,
and both map to BankDataResource:

// src/Dto/BankDataDto.php
namespace App\Dto;

use App\Api\BankDataResource;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: BankDataResource::class)]
class BankDataDto
{
    public string $iban;
    public BankDto $bank;
}

// src/Dto/BankDto.php
namespace App\Dto;

use App\Api\BankDataResource;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: BankDataResource::class)]
class BankDto
{
    #[Map(target: 'bic')]
    public string $bic;

    #[Map(target: 'bankCode')]
    public string $code;

    #[Map(target: 'bankName')]
    public string $name;
}

// src/Entity/BankDataResource.php
namespace App\Api;

class BankDataResource
{
    public string $iban;
    public string $bic;
    public string $bankCode;
    public string $bankName;
}

When mapping BankDataDto, 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 = new BankDto();
$bankDto->bic = 'BIC123';
$bankDto->code = 'BANK001';
$bankDto->name = 'Test Bank';

$dto = new BankDataDto();
$dto->iban = 'IBAN12345';
$dto->bank = $bankDto;

$mapper = new ObjectMapper();
$resource = $mapper->map($dto, BankDataResource::class);

// $resource->iban === 'IBAN12345'
// $resource->bic === 'BIC123' (mapped from the nested BankDto)
// $resource->bankCode === 'BANK001'
// $resource->bankName === 'Test Bank'

@carsonbot carsonbot added this to the 7.4 milestone Nov 25, 2025
@soyuka soyuka changed the base branch from 7.4 to 8.0 November 26, 2025 08:56
@soyuka soyuka force-pushed the object-mapper-nested-2 branch 2 times, most recently from 71a147f to 027c81d Compare November 26, 2025 08:58
@soyuka
Copy link
Contributor Author

soyuka commented Nov 26, 2025

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

@OskarStark OskarStark modified the milestones: 7.4, 8.1 Nov 29, 2025
@soyuka soyuka changed the base branch from 8.0 to 8.1 December 1, 2025 07:24
@soyuka soyuka force-pushed the object-mapper-nested-2 branch from 027c81d to c0a2426 Compare December 1, 2025 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants