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

How do you handle source Collection prop mapped to target array prop? #222

Open
fpetrovic opened this issue Feb 2, 2025 · 3 comments
Open

Comments

@fpetrovic
Copy link

Is there any way I can map Collection of entity objects to array of dto objects?

@Korbeil
Copy link
Member

Korbeil commented Feb 14, 2025

Hey, this is actually something that works with AutoMapper, could you give me an example to understand your issue ?

For a working example, you can check at https://github.com/jolicode/automapper/blob/main/tests/AutoMapperTest.php#L1631-L1646 which is a test that map a Doctrine Collection to an array.

@fpetrovic
Copy link
Author

fpetrovic commented Feb 27, 2025

Hello @Korbeil I had to add add and remove methods, but now it works in this scenario where name of dto and entity property is exact:
` /**
* @var NotificationGroupApi[] $notificationGroups
*/

#[Groups(['record:item:read', 'record:list'])]

private array $notificationGroups;`

When property names are not the same MapFrom does not help me at all
` /**
* @var MediaApi[] $media
*/

#[Groups(['record:item:read', 'record:list'])]

#[MapFrom(property: 'recordMedia')]

private array $media;`

I have adders and removers for both and both are included into serialization group. First works, second does not work.

@fpetrovic
Copy link
Author

fpetrovic commented Feb 28, 2025

I think I found the problem:
app/vendor/symfony/property-info/Extractor/ReflectionExtractor.php:409
$singulars = $this->inflector->singularize($camelized);
It singularizes plural of property name and "Media" plural was singularized as "Medium" .... That's why my adders and removers were not recognized "addMedia" "removeMedia"...

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