Skip to content

[ObjectMapper] Reverse side mapping and reflection property #60058

@loverg-c

Description

@loverg-c

Symfony version(s) affected

7.3

Description

Hello,

I wanted to test the new experimental feature ObjectMapper by trying to Map from the reverse side of the mapped object (like that :

class A
{
    public ?\DateTimeImmutable $beginAt;
}

#[Map(target: self::class, source: A::class)]
class B
{
    public ?\DateTimeImmutable $beginAt;
}

) which is maybe not supported for the moment I guess.

Anyway, the problem is that it is throwing a MappingException:

Property App\A::$Property [ public ?DateTimeImmutable $beginAt ] does not exist

in vendor/symfony/object-mapper/Metadata/ReflectionObjectMapperMetadataFactory.php (line 42)
in[ vendor/symfony/object-mapper/ObjectMapper.php ](file:///app/vendor/symfony/object-mapper/ObjectMapper.php#L301)-> create (line 301)
....

I think it is because of the method ReflectionObjectMapperMetadataFactory::create which is waiting a ?string $property = null , but in my specific case i have a ReflectionProperty type (which may transform it to string to Property App\A::$Property [ public ?DateTimeImmutable $beginAt ]).

It seems to work when I use $property->name instead on ObjectMapper.php#L301, but i'm not sure if it's the awaited solution neither if the reverse side mapping is an intended feature.

(btw thanks for this awesome future component !)

How to reproduce

class A
{
    public ?\DateTimeImmutable $beginAt;
}

#[Map(target: self::class, source: A::class)]
class B
{
    public ?\DateTimeImmutable $beginAt;
}

Possible Solution

Check if the property is instance of ReflectionProperty and if so use $property->name instead

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions