The ObjectMapper component copies data between objects, such as DTOs and
entities, based on #[Map] attributes. It was introduced in Symfony 7.3
and improved in Symfony 8.1. Symfony 8.2 removes more of the glue code that
you still had to write for common cases and makes mappings much faster.
Automatic Backed Enum Conversion
Entities often store values as backed enums, while the DTOs around them (API payloads, Messenger commands, view models) use the scalar value. Mapping between both required a custom transformer in each direction. In Symfony 8.2, the mapper compares the types of both properties and converts the value for you:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
namespace App\Message;
use App\Entity\Post;
use Symfony\Component\ObjectMapper\Attribute\Map;
#[Map(target: Post::class)]
final class CreatePost
{
public function __construct(
public string $title,
public string $status,
) {
}
}
1 2 3 4 5
// App\Entity\Post defines this property:
// public PostStatus $status;
// and PostStatus is a string-backed enum with cases like Draft = 'draft'
$post = $objectMapper->map(new CreatePost('Hello world', 'draft'));
// $post->status === PostStatus::Draft
The conversion works both ways (string or int to enum and enum to
string or int) and also when the property is renamed with
#[Map(target: '...')].
Nested Targets Inferred From Property Types
Symfony 8.1 let you define mappings on the target class with
#[Map(source: ...)]. However, the mapper didn't always apply those mappings
to nested objects: outside of the class map built by the framework, a nested
object was copied as is, which failed with a TypeError when the target
property expected another class.
In Symfony 8.2, when a nested object has no mapping of its own, the mapper looks
at the type of the property it's written into and uses the #[Map]
attributes of that class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
use App\Entity\Category;
use App\Entity\Product;
use Symfony\Component\ObjectMapper\Attribute\Map;
#[Map(source: Product::class)]
final class ProductView
{
public string $name;
// $product->category is a Category entity; the mapper sees that
// CategoryView maps from Category and creates the view for you
public CategoryView $category;
}
#[Map(source: Category::class)]
final class CategoryView
{
public string $name;
}
1 2
$view = $objectMapper->map($product, ProductView::class);
// $view->category is a CategoryView
This requires no configuration and applies only to properties typed with a
single class or interface. Explicit #[Map] attributes and the class map
always take precedence.
Merging Repeated Mappings Into the Same Object
The #[Map] attribute is repeatable, so a target property can be built from
several source properties. Until now, when those sources were objects, each
mapped object replaced the previous one and only the last one was kept. In
Symfony 8.2, the mapper creates the object for the first source and maps the
other ones into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
use Symfony\Component\ObjectMapper\Attribute\Map;
#[Map(target: Profile::class)]
final class Identity
{
public string $name;
}
#[Map(target: Profile::class)]
final class Contact
{
public string $email;
}
final class Profile
{
public ?string $name = null;
public ?string $email = null;
}
final class CustomerView
{
// $customer->identity and $customer->contact are
// merged into a single Profile object
#[Map(source: 'identity')]
#[Map(source: 'contact')]
public Profile $profile;
}
1 2 3
$view = $objectMapper->map($customer, CustomerView::class);
// before: $view->profile only had the email
// after: $view->profile has the name and the email
Mapping-Aware Transformers
Transformers receive the value, the source object and the target object, but they don't know which mapping they are running for. That's a problem for transformers shared by several properties, which had to be duplicated or configured per property.
Symfony 8.2 adds MappingAwareTransformCallableInterface, which extends
TransformCallableInterface to pass the #[Map] attribute being applied as
the fourth argument:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
namespace App\ObjectMapper;
use Symfony\Component\ObjectMapper\Attribute\Map;
use Symfony\Component\ObjectMapper\MappingAwareTransformCallableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
final class LabelTransformer implements MappingAwareTransformCallableInterface
{
public function __construct(
private TranslatorInterface $translator,
) {
}
public function __invoke(mixed $value, object $source, ?object $target, ?Map $mapping = null): mixed
{
// e.g. 'order.statusLabel.shipped' or 'order.paymentLabel.refunded'
return $this->translator->trans(sprintf('order.%s.%s', $mapping?->target, $value));
}
}
1 2 3 4 5 6 7 8 9 10 11 12
use App\ObjectMapper\LabelTransformer;
use Symfony\Component\ObjectMapper\Attribute\Map;
#[Map(target: OrderView::class)]
class Order
{
#[Map(target: 'statusLabel', transform: LabelTransformer::class)]
public string $status;
#[Map(target: 'paymentLabel', transform: LabelTransformer::class)]
public string $paymentStatus;
}
The mapper always passes this argument; it's nullable only because of PHP
signature rules. Its target is null when the mapping is defined on the
target class with #[Map(source: ...)].
Faster Mappings With Precomputed Metadata
On every map() call, the mapper worked out again facts that only depend on
the source and target classes: the properties to visit, the #[Map]
attributes that apply to them, how to read and write each property, etc.
In Symfony 8.2, this metadata is computed once per pair of classes and reused.
In the benchmark of the pull request (a DTO with 20 properties and two nested
objects), mappings are 1.7 to 1.9 times faster from the second map() call
of the same pair of classes. This benefits any request that maps several objects
of the same class and, even more, long-running processes like Messenger workers
and FrankenPHP in worker mode.
The first map() call of each pair is the expensive one, so Symfony
applications now also compute the metadata of all the classes that define
#[Map] attributes while warming up the cache and store it in a PHP file
served by OPcache. With it, that first call is about 20% faster than before too.
As with the Serializer metadata, this cache is disabled in debug mode, so changes
in your attributes are applied immediately.