Skip to content

Conversation

@daFish
Copy link

@daFish daFish commented Nov 13, 2025

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

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

@daFish daFish marked this pull request as ready for review November 13, 2025 08:26
@carsonbot carsonbot added this to the 7.4 milestone Nov 13, 2025
@daFish daFish force-pushed the feat/object-mapper-nested-objects branch from ad52d38 to 3b71652 Compare November 13, 2025 08:27
@soyuka
Copy link
Contributor

soyuka commented Nov 13, 2025

I don't understand, we support nested object mapping from the beginning https://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 at https://github.com/symfony/symfony/blob/7.4/src/Symfony/Component/ObjectMapper/ObjectMapper.php#L231-L261 and in a recursive manner.

@daFish daFish force-pushed the feat/object-mapper-nested-objects branch from 3b71652 to 3794a99 Compare November 13, 2025 09:54
@daFish
Copy link
Author

daFish commented Nov 13, 2025

@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

soyuka commented Nov 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
Copy link
Author

daFish commented Nov 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

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

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

daFish commented Nov 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
Copy link
Contributor

soyuka commented Nov 17, 2025

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

@daFish
Copy link
Author

daFish commented Nov 17, 2025

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

@temp
Copy link

temp commented Nov 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
Copy link
Contributor

soyuka commented Nov 17, 2025

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 commented Nov 18, 2025

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 commit 1e22b511d1a219099c89d39c2c4d6ce340566cf7. Using the symfony/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.

<?php

declare(strict_types=1);

use Symfony\Component\ObjectMapper\Attribute\Map;
use Symfony\Component\ObjectMapper\ObjectMapper;

require __DIR__ . '/vendor/autoload.php';

final readonly class BMapped {
    public function __construct(
        public string $var2,
    ) {}
}

final readonly class AMapped {
    public function __construct(
        public BMapped $b,
        public string $var1,
    ) {}
}

#[Map(target: BMapped::class)]
final readonly class B {
    public function __construct(
        public string $var2,
    ) {}
}

#[Map(target: AMapped::class)]
final readonly class A {
    public function __construct(
        public B $b,
        public string $var1,
    ) {}
}

/** @var AMapped $out */
$out = new ObjectMapper()->map(
    source: new A(
        b: new B(
            var2: 'bar',
        ),
        var1: 'foo',
    ),
);
var_dump($out);
echo $out->b->var2;

Output before commit 1e22b511:

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 commit 1e22b511:

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:189
Stack 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 commit 1e22b511, using symfony/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:542
Stack 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 commented Nov 19, 2025

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 DTO
readonly class UserDto
{
    public function __construct(
        #[Map(target: 'address.zipcode')] public bool $userAddressZipcode,
        #[Map(target: 'address.city')] public bool $userAddressCity
    ) {
    }
}
// Target User entity
class User
{
    #[ORM\Embedded(Address::class)]
    private Address $address
}
// Some controller
public function doStuff(UserDto $dto) 
{
    $this->objectMapper->map(source: $dto, target: User::class);
}

@soyuka
Copy link
Contributor

soyuka commented Nov 19, 2025

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

@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

soyuka commented Nov 25, 2025

@daFish is #62455 fixing your issue?

@daFish
Copy link
Author

daFish commented Nov 25, 2025

@daFish is #62455 fixing your issue?

Will be checked today.

@temp
Copy link

temp commented Nov 25, 2025

@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 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] Subobjects are not mapped

7 participants