Open
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.
$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:
// 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.