-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Nested mapping, where collection element needs to have parent assigned to its property #16
Comments
You are right, you can't do this with the Automapper (yet). It is, however, an interesting usecase. I have some ideas on how to resolve this, but I'm gonna need some time to test things out. Thanks for bringing this under my attention, I'll keep you posted! |
Hey mark, any news about this? Would love to hear about your ideas at least! |
Hi @ostrolucky, I've been playing around with this for a while, but I haven't found a good solution. My idea was the following:
I was able to implement this, and it worked just fine for your use case (child properties referencing their parent). It was, however, far from perfect. Some problems encountered:
I think it is therefore best to not resolve references automagically, but instead we could rely on some mechanism to explicitly define references you want to keep. Maybe something using the Symfony property accessor. It could look something like this:
This is just an idea, I don't know if it would be possible to implement. I can see some problems with this approach:
So, as a conclusion, I'm kind of stuck on this. Automatically resolving references doesn't look like a good idea, and manually defining references might take a long while to implement, if it's even possible. I'm going to experiment a bit with this though. In the meanwhile I'm open to other suggestions :) I'm sorry I can't help you with this for now. |
Agreed 100%. I'm definitely not asking to try to guess this automatically, that would produce false positives. I just want some way to configure mapping for this use case. Something I had in mind was to allow to use custom closures where we could do anything we want. In this example, it could be used following way: $config->registerMapping(Child::class, Child::class)
->forMember('parent', Operation::custom(function(Child $child, Parent $parent) {
return $parent;
}) or even for arrays via parent $config->registerMapping(Parent::class, Parent::class)
->forMember('children', Operation::custom(function(Parent $parent) {
$out = [];
foreach ($parent->getChildren() as $child) {
$child = $this->automapper->map($child, Child::class);
$child->setParent($parent);
$out[] = $child;
}
return $out;
}) I like PropertyAccess component usage idea as well, mainly because I would wish automapper used it in the first place, as that interface is more standard. You could just decorate it and use reflection as you do now. This is already done in some projects, e.g. nelmio/alice. Even better, use alternative Closure::bind approach, which is faster. Ultimately, for use cases like in this issue, custom closure is most flexible though. |
That's a really great suggestion. I'll definitely look into using a decorated PropertyAccess component. nelmio/alice is a great example as well. For the problem with the references, however, I don't see how your example could be a solution. Normally, an operation would receive the object being mapped. So |
Yes, idea is to pass resulting object and its parent |
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.
Using Operation::mapTo like following is close, but $optionsDTO#answerSet will be null, or it will just copy same object over:
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.The text was updated successfully, but these errors were encountered: