Skip to content

Conversation

@soyuka
Copy link
Contributor

@soyuka soyuka commented May 17, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues Related to #60432 also fixes #61116
License MIT

This is another approach to embeded collection mapping that avoids changing the ObjectMapper. Even if this doesn't land in the component it'd be a good thing to document. Let me know your thoughts.

@soyuka soyuka force-pushed the map-collection branch 2 times, most recently from 36eef03 to 2da896a Compare May 19, 2025 08:49
@soyuka
Copy link
Contributor Author

soyuka commented Jul 16, 2025

We're already using this on a project and it works quite well.

@crtl
Copy link
Contributor

crtl commented Aug 4, 2025

How do you use it? How is this supposed when using sources only?
Ive tried using MapCollection transformer but it will throw exception:

class UserDto {
/**
     * @var OrganizationMembershipDto[]
     */
    #[Map(transform: new MapCollection(), source: "memberships")]
    public array $memberships;
    
}
$dto = $this->mapper->map($user, UserDto::class);

will throw:

Symfony\Component\ObjectMapper\Exception\MappingException : Mapping target not found for source "App\Entity\User\OrganizationMembership".
 /var/www/vendor/symfony/object-mapper/ObjectMapper.php:58
 /var/www/src/Dto/Response/ObjectMapper/Transformer/MapCollection.php:30
 /var/www/vendor/symfony/object-mapper/ObjectMapper.php:225
 /var/www/vendor/symfony/object-mapper/ObjectMapper.php:259
 /var/www/vendor/symfony/object-mapper/ObjectMapper.php:176
 /var/www/vendor/symfony/object-mapper/ObjectMapper.php:139

Like ive mentioned in #61116 theres is currently no API in object mapper to provide metadata to transformers and without this transformers are unable to guess type of target value.
Having to set the target on the source breaks SRP and erodes the actual purpose of object mapper.

TDLR; Only works when mapping target is defined on source.

@soyuka
Copy link
Contributor Author

soyuka commented Aug 6, 2025

@crtl related to #61310

@soyuka soyuka force-pushed the map-collection branch 2 times, most recently from 39aab74 to 68b8bdd Compare August 19, 2025 13:52
@fabpot
Copy link
Member

fabpot commented Aug 23, 2025

Thank you @soyuka.

@fabpot fabpot merged commit cd42aac into symfony:7.4 Aug 23, 2025
10 of 12 checks passed
@klkvsk
Copy link

klkvsk commented Sep 15, 2025

Am I right there is currently no way to specify which target class a collection item should be mapped to, in case when there are multiple mapped classes? So even if I make my own MapCollection transformer, the callback signature __invoke(mixed $value, object $source, ?object $target) is missing any mapping info about the property, so I'm unable to determine the mapped type.

@soyuka
Copy link
Contributor Author

soyuka commented Sep 15, 2025

in case when there are multiple mapped classes

This is not supported, this transformer only transforms to a collection of a single type. You can create your own transform and gather type at runtime but this is not optimal. Please open a new issue and detail your usage.

This was referenced Oct 27, 2025
@mdriessen
Copy link

@soyuka The problem isn't about a mixed collection type but rather multiple possible collections of a single type. Tried to explain the problem I'm having in more detail:
#61957 (comment)

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.

[ObjectMapper] Support mapping to nested arrays of objects