Skip to content

Commit 4cb6ca8

Browse files
committed
minor #21320 [ObjectMapper] embed collection transformer (soyuka)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [ObjectMapper] embed collection transformer Fix #21314 * #21314 Commits ------- ba921fb [ObjectMapper] embed collection transformer
2 parents 0582836 + ba921fb commit 4cb6ca8

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)