@@ -691,6 +691,26 @@ your DTOs/Entities, you can implement a custom mapping strategy using the
691691This allows defining mapping rules within dedicated mapper services, similar
692692to the approach used by libraries like MapStruct in the Java ecosystem.
693693
694+ .. note ::
695+
696+ Symfony's built-in ``#[Map] `` attribute only supports ``TARGET_CLASS `` and
697+ ``TARGET_PROPERTY `` targets. If you want to use ``#[Map] `` on methods (as in
698+ the MapStruct-like example below), create a custom attribute that extends
699+ Symfony's ``Map `` attribute and adds ``TARGET_METHOD `` support::
700+
701+ // src/ObjectMapper/Attribute/Map.php
702+ namespace App\ObjectMapper\Attribute;
703+
704+ use Symfony\Component\ObjectMapper\Attribute\Map as BaseMap;
705+
706+ #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
707+ class Map extends BaseMap
708+ {
709+ }
710+
711+ Then, use this custom ``App\ObjectMapper\Attribute\Map `` attribute instead of
712+ Symfony's built-in one in your mapper classes.
713+
694714First, create your custom metadata factory. The following example reads mapping
695715rules defined via ``#[Map] `` attributes on a dedicated mapper service class,
696716specifically on its ``map `` method for property mappings and on the class itself
@@ -753,14 +773,15 @@ for the source-to-target relationship::
753773Next, define your mapper service class. This class implements ``ObjectMapperInterface ``
754774but typically delegates the actual mapping back to a standard ``ObjectMapper ``
755775instance configured with the custom metadata factory. Mapping rules are defined
756- using ``#[Map] `` attributes on this class and its ``map `` method::
776+ using your custom ``#[Map] `` attribute (not Symfony's built-in one) on this class
777+ and its ``map `` method::
757778
758779 namespace App\ObjectMapper;
759780
760781 use App\Dto\LegacyUser;
761782 use App\Dto\UserDto;
783+ use App\ObjectMapper\Attribute\Map;
762784 use App\ObjectMapper\Metadata\MapStructMapperMetadataFactory;
763- use Symfony\Component\ObjectMapper\Attribute\Map;
764785 use Symfony\Component\ObjectMapper\ObjectMapper;
765786 use Symfony\Component\ObjectMapper\ObjectMapperInterface;
766787
0 commit comments