Skip to content

Commit ba921fb

Browse files
soyukajaviereguiluz
authored andcommitted
[ObjectMapper] embed collection transformer
1 parent 6b6a678 commit ba921fb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

object_mapper.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,32 @@ And the related target object must define the ``createFromLegacy()`` method::
428428
}
429429
}
430430

431+
Mapping Collections
432+
-------------------
433+
434+
By default, ObjectMapper does not map arrays or traversable collections.
435+
To map each item in a collection (such as an array of DTOs to an array of entities), you **must** use the `MapCollection` transformer explicitly:
436+
437+
Example::
438+
439+
use Symfony\Component\ObjectMapper\Attribute\Map;
440+
use Symfony\Component\ObjectMapper\Transform\MapCollection;
441+
442+
class ProductListInput
443+
{
444+
#[Map(transform: new MapCollection())]
445+
/** @var ProductInput[] */
446+
public array $products;
447+
}
448+
449+
This configuration tells ObjectMapper to map each item in the `products` array using the usual mapping rules.
450+
451+
If you do not add `transform: new MapCollection()`, the array will be mapped as-is.
452+
453+
.. versionadded:: 7.4
454+
455+
The MapCollection component was introduced in Symfony 7.4.
456+
431457
Mapping Multiple Targets
432458
------------------------
433459

0 commit comments

Comments
 (0)