Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.8k
[ObjectMapper] Add support for mapping nested objects#62377
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
base:8.1
Are you sure you want to change the base?
Conversation
carsonbot commentedNov 13, 2025
Hey! To help keep things organized, we don't allow "Draft" pull requests. Could you please click the "ready for review" button or close this PR and open a new one when you are done? Note that a pull request does not have to be "perfect" or "ready for merge" when you first open it. We just want it to be ready for a first review. Cheers! Carsonbot |
ad52d38 to3b71652Comparesoyuka commentedNov 13, 2025
I don't understand, we support nested object mapping from the beginninghttps://github.com/symfony/symfony/blob/7.4/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php#L105-L113 |
| } | ||
| $nestedValue =$this->getSourceValue($value,$mappedTarget,$nestedValue,$objectMap,$nestedMapping); | ||
| $this->storeValue($nestedTargetPropertyName,$mapToProperties,$ctorArguments,$nestedValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
we should not do this, it's already happening athttps://github.com/symfony/symfony/blob/7.4/src/Symfony/Component/ObjectMapper/ObjectMapper.php#L231-L261 and in a recursive manner.
3b71652 to3794a99ComparedaFish commentedNov 13, 2025
soyuka commentedNov 13, 2025
Wait but I think I misunderstood here's how I'd do this: https://github.com/soyuka/symfony/pull/new/feat/object-mapper-nested-objects |
daFish commentedNov 13, 2025
Much more elegant. We‘ll test this tomorrow and provide feedback. |
daFish commentedNov 17, 2025
@soyuka Your solution works as expected. Only complain I'd have is, that it is a bit cryptic. A note in the class-level docblock could be helpful to understand the purpose of the class. |
soyuka commentedNov 17, 2025
@daFish not sure that we should contribute this, maybe an entry in the docs would be sufficient? |
daFish commentedNov 17, 2025
@soyuka This use case should work out of the box IHMO making it a valuable addition. |
temp commentedNov 17, 2025
I'm also of the opinion that this use case should work out-of-the-box. Having nested objects isn't such an uncommon thing. |
soyuka commentedNov 17, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Okay I've a lot of work these days so I won't be able to do this soon, do you think we should propose the transform or instead we should discover automatically that the property must be read on the embeded object? (btw feel free to take my suggestion and update your PR :)) |
temp commentedNov 18, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I'd vote for the latter, discover automatically. But transform is fine, as well, as long as it works without implementing your own workarounds... |
meinemitternacht commentedNov 18, 2025
@soyuka I am developing a new project at work and ran across some similar issues. However, in our case, we had nested object maps working before commit1e22b511d1a219099c89d39c2c4d6ce340566cf7. Using the <?phpdeclare(strict_types=1);useSymfony\Component\ObjectMapper\Attribute\Map;useSymfony\Component\ObjectMapper\ObjectMapper;require__DIR__ .'/vendor/autoload.php';finalreadonlyclass BMapped {publicfunction__construct(publicstring$var2, ) {}}finalreadonlyclass AMapped {publicfunction__construct(publicBMapped$b,publicstring$var1, ) {}}#[Map(target: BMapped::class)]finalreadonlyclass B {publicfunction__construct(publicstring$var2, ) {}}#[Map(target: AMapped::class)]finalreadonlyclass A {publicfunction__construct(publicB$b,publicstring$var1, ) {}}/** @var AMapped $out */$out =newObjectMapper()->map( source:newA( b:newB( var2:'bar', ), var1:'foo', ),);var_dump($out);echo$out->b->var2; Output before commit Output after commit Output after commit |
siggidiel commentedNov 19, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hey guys. Maybe I'm blind or this request here is just for mapping nested objects of the source to flat properties of a target. But I have the reversed case and I can't find a way how to do that. I hoped to be able to do something like a dot notation. And I also can't figure out how to make it work with a custom transformer. I don't want to implement a custom mapping strategy as this will overcomplicate things in our code. Any idea? Example: |
soyuka commentedNov 19, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@meinemitternacht can you please open a new issue?@siggidiel could you open a new issue ? |
siggidiel commentedNov 19, 2025
Done:#62439 |
meinemitternacht commentedNov 19, 2025
|
This feature adds support to map nested objects as reported in#62357. As reported by@temp, this does not work with the current component.