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] 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

Open
daFish wants to merge2 commits intosymfony:8.1
base:8.1
Choose a base branch
Loading
fromdaFish:feat/object-mapper-nested-objects

Conversation

@daFish
Copy link

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

This feature adds support to map nested objects as reported in#62357. As reported by@temp, this does not work with the current component.

@carsonbot
Copy link

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

@daFishdaFish marked this pull request as ready for reviewNovember 13, 2025 08:26
@carsonbotcarsonbot added this to the7.4 milestoneNov 13, 2025
@daFishdaFishforce-pushed thefeat/object-mapper-nested-objects branch fromad52d38 to3b71652CompareNovember 13, 2025 08:27
@soyuka
Copy link
Contributor

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);
Copy link
Contributor

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.

@daFishdaFishforce-pushed thefeat/object-mapper-nested-objects branch from3b71652 to3794a99CompareNovember 13, 2025 09:54
@daFish
Copy link
Author

@soyuka I have debugged a bit with@temp and we come to the conclusion, that it does not work without the patch. I have split up the tests from the change. Running without the patched code the tests fail.

@soyuka
Copy link
Contributor

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
Copy link
Author

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

Much more elegant. We‘ll test this tomorrow and provide feedback.

@nicolas-grekasnicolas-grekas modified the milestones:7.4,8.1Nov 16, 2025
@daFish
Copy link
Author

@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
Copy link
Contributor

@daFish not sure that we should contribute this, maybe an entry in the docs would be sufficient?

@daFish
Copy link
Author

@soyuka This use case should work out of the box IHMO making it a valuable addition.

@temp
Copy link

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
Copy link
Contributor

soyuka commentedNov 17, 2025
edited
Loading

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
Copy link

temp commentedNov 18, 2025
edited
Loading

I'd vote for the latter, discover automatically. But transform is fine, as well, as long as it works without implementing your own workarounds...
We used the transform-variant in our project, works fine.

@meinemitternacht
Copy link

@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 thesymfony/property-access component changes the error message, but the overall result is the same. If this is not related, I can open a separate issue.

<?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 commit1e22b511:

class AMapped#9 (2) {  public readonly BMapped $b =>  class BMapped#19 (1) {    public readonly string $var2 =>    string(3) "bar"  }  public readonly string $var1 =>  string(3) "foo"}bar

Output after commit1e22b511:

class AMapped#9 (2) {  public readonly BMapped $b =>  class BMapped#22 (1) {    public readonly string $var2 =>    *uninitialized*  }  public readonly string $var1 =>  string(3) "foo"}PHP Fatal error:  Uncaught Error: Cannot modify readonly property BMapped::$var2 in /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/vendor/symfony/object-mapper/ObjectMapper.php:189Stack trace:#0 /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/vendor/symfony/object-mapper/ObjectMapper.php(49): Symfony\Component\ObjectMapper\ObjectMapper->doMap()#1 /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/vendor/symfony/object-mapper/ObjectMapper.php(253): Symfony\Component\ObjectMapper\ObjectMapper->map()#2 /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/test.php(48): Symfony\Component\ObjectMapper\ObjectMapper->{closure:Symfony\Component\ObjectMapper\ObjectMapper::getSourceValue():249}()#3 {main}  thrown in /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/vendor/symfony/object-mapper/ObjectMapper.php on line 189

Output after commit1e22b511, usingsymfony/property-access:

class AMapped#22 (2) {  public readonly BMapped $b =>  class BMapped#37 (1) {    public readonly string $var2 =>    *uninitialized*  }  public readonly string $var1 =>  string(3) "foo"}PHP Fatal error:  Uncaught Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException: The property "var2" in class "BMapped" is a promoted readonly property.. in /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/vendor/symfony/property-access/PropertyAccessor.php:542Stack trace:#0 /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/vendor/symfony/property-access/PropertyAccessor.php(120): Symfony\Component\PropertyAccess\PropertyAccessor->writeProperty()#1 /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/vendor/symfony/object-mapper/ObjectMapper.php(189): Symfony\Component\PropertyAccess\PropertyAccessor->setValue()#2 /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/vendor/symfony/object-mapper/ObjectMapper.php(49): Symfony\Component\ObjectMapper\ObjectMapper->doMap()#3 /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/vendor/symfony/object-mapper/ObjectMapper.php(253): Symfony\Component\ObjectMapper\ObjectMapper->map()#4 /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/test.php(56): Symfony\Component\ObjectMapper\ObjectMapper->{closure:Symfony\Component\ObjectMapper\ObjectMapper::getSourceValue():249}()#5 {main}  thrown in /home/meinemitternacht/projects/symfony-object-mapper-test-1e22b511/vendor/symfony/property-access/PropertyAccessor.php on line 542

@siggidiel
Copy link

siggidiel commentedNov 19, 2025
edited
Loading

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:

// My source DTOreadonly class UserDto{    public function __construct(        #[Map(target: 'address.zipcode')] public bool $userAddressZipcode,        #[Map(target: 'address.city')] public bool $userAddressCity    ) {    }}
// Target User entityclass User{    #[ORM\Embedded(Address::class)]    private Address $address}
// Some controllerpublic function doStuff(UserDto $dto) {    $this->objectMapper->map(source: $dto, target: User::class);}

@soyuka
Copy link
Contributor

soyuka commentedNov 19, 2025
edited
Loading

@meinemitternacht can you please open a new issue?@siggidiel could you open a new issue ?

siggidiel reacted with thumbs up emoji

@siggidiel
Copy link

@meinemitternacht can you please open a new issue?@siggidiel could you open a new issue ?

Done:#62439

@meinemitternacht
Copy link

@meinemitternacht can you please open a new issue?@siggidiel could you open a new issue ?

#62442

@soyuka
Copy link
Contributor

@daFish is#62455 fixing your issue?

@daFish
Copy link
Author

@daFish is#62455 fixing your issue?

Will be checked today.

@temp
Copy link

@daFish is#62455 fixing your issue?

@soyuka that doesn't solve our problem (reading nested properties with target annotation), but solves another problem that we have (setting nested properties with target annotation). PR code works as expected!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@soyukasoyukasoyuka left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

8.1

Development

Successfully merging this pull request may close these issues.

[ObjectMapper] Subobjects are not mapped

7 participants

@daFish@carsonbot@soyuka@temp@meinemitternacht@siggidiel@nicolas-grekas

[8]ページ先頭

©2009-2025 Movatter.jp