Cannot use getters and setters with symfony/object-mapper #62750
-
|
I'm porting an application to a three-tier architecture (repository/service/controller). I want entities from Doctrine repositories to never be exposed to controllers but rather being mapped into DTOs which then get passed to controllers. To map fields from entities to DTOs I'd like to use the new symfony/object-mapper but I'm encountering issues. At the moment I have the fields in Doctrine entities exposed trough standard getters/setters private string $my_field;
public function getMyField(): string
{
return $this->my_field;
}
public function setMyField(string $my_field): static
{
$this->my_field = $my_field;
return $this;
}but Symfony Object Mapper seems to totally ignore getters and setters so when I try to map entity's fields into their equivalent within DTO using the following statement $dto = $mapper->map($entity, EntityName::class);I always end up getting exceptions:
In the official docs the only mention of getters/setters is in the last paragraph hinting at the possibility of configuring the mapper with a Can I make getters/setters recognizable to object mapper? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
If you use the ObjectMapper service configured by FrameworkBundle, the usage of PropertyAccessorInterface is enabled automatically when the If you instantiate the ObjectMapper yourselves, you should inject an instance of PropertyAccessorInterface in the constructor arguments. |
Beta Was this translation helpful? Give feedback.
If you use the ObjectMapper service configured by FrameworkBundle, the usage of PropertyAccessorInterface is enabled automatically when the
property-accesscomponent is installed.If you instantiate the ObjectMapper yourselves, you should inject an instance of PropertyAccessorInterface in the constructor arguments.