mark-gerarts / mark-gerarts/automapper-plus
Nested mapping, where collection element needs to have parent assigned to its property
- Dominant language
- PHP
- Stars
- 562
- Forks
- 32
- PR merge metrics
- No merged PRs in 30d
Description
In following example, we didn't find a way how to replicate following in Automapper. We don't think it's possible without very custom things.
```php
$answerSetDTO = ...
$duplicatedAnswerSetDTO = $this->autoMapper->map($answerSetDTO, AnswerSetDTO::class);
$optionsDTO = [];
foreach ($answerSetDTO->options as $optionDTO) {
$optionDTO = $this->autoMapper->map($optionDTO, AnswerSetOptionDTO::class);
$optionDTO->answerSet = $duplicatedAnswerSetDTO; // <- here is a complication
$optionsDTO[] = $optionDTO;
}
$duplicatedAnswerSetDTO->options = new ArrayCollection($optionsDTO);
```
Using Operation::mapTo like following is close, but $optionsDTO#answerSet will be null, or it will just copy same object over:
```php
// cloning
$autoMapperConfig->registerMapping(AnswerSetDTO::class, AnswerSetDTO::class)
->forMember('id', Operation::ignore())
->forMember('options', Operation::mapTo(AnswerSetOptionDTO::class))
->forMember('questions', Operation::ignore());
$autoMapperConfig->registerMapping(AnswerSetOptionDTO::class, AnswerSetOptionDTO::class)
->forMember('id', Operation::ignore())
// here is second culprit. Causes answerSet to be null,
// or same object instead of parent one if Operation::ignore() is removed
// how to assign parent object id instead?
->forMember('answerSet', Operation::ignore());
```
So, if `$answerSetDTO#options[0]#answerSet#id = 1` and `$duplicateAnswerSetDTO#id = 2`, we want `$duplicateAnswerSetDTO#options[0]#answerSet#id = 2`. Currently, we can achieve it to be null or 1 only.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the nested AnswerSetDTO to AnswerSetOptionDTO mapping using the registerMapping calls and Operation::mapTo example in the issue. Trace how the parent object is handled during collection mapping, then verify that each duplicated option references the duplicated parent with its new id rather than the source parent or null.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100