Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing target data in Custom Transformer #172

Open
cpoint-eu opened this issue Jun 27, 2024 · 3 comments
Open

Missing target data in Custom Transformer #172

cpoint-eu opened this issue Jun 27, 2024 · 3 comments

Comments

@cpoint-eu
Copy link

It would be great to have access to the target object's data and the specific property being transformed in the transform method of a custom transformer. This would greatly help in creating more generic data transformation rules.

If I have a data field that contains IDs instead of specific objects, and I want to create a generic transformer to translate IDs into objects, I currently have to create a separate transformer for each transformation. The only difference between these transformers is the className, which seems unnecessary to me. If I had access to the target object's data, I could determine this directly within it (similarly to the support method).

@cpoint-eu cpoint-eu changed the title Missing Data in Custom Transformer Missing target data in Custom Transformer Jun 27, 2024
@AntipodTX
Copy link

AntipodTX commented Oct 23, 2024

Same issue here.

My case:

I map the DTO, containing some references as integer IDs to the Doctrine Entity, containing related entities of the different types - so i want to create a correspondent Entity based on its ID for every DTO/Entity property pair. Now, i have no possibility to determine the target Entity class with this concrete ID.

My proposal:

Give the array|null return type to the supports() method (where we have the full access to the source and the target metadata, null = 'not supported') to have it written in the generated mapper class (and cached) and have it as a fourth parameter of the transform() method (or have a separated method for that purpose with the same input parameters as support() - it will be even better). Using this, the property mappers can be more flexible.

@MrMeshok
Copy link
Contributor

I think you can use TransformerFactory for that, they get all needed metadata. For example something like that

class GenericTransformerFactory
{
    public function getTransformer(TypesMatching $types, SourcePropertyMetadata $source, TargetPropertyMetadata $target, MapperMetadata $mapperMetadata): ?TransformerInterface
    {
        $targetType = $types->getTargetUniqueType($types->getSourceUniqueType());

        return new GenericTransformer($targetType->getClassName());
    }
}

But transformerFactories argument in AutoMapper::create() is deprecated, I am not sure why. And I am not familiar with Symfony, so don't know how to inject custom factories there

@AntipodTX
Copy link

@MrMeshok Thank you for your advice!
As i see, it should not be difficult to override the TransformerFactory in Symfony - the automapper bundle already implements a automapper.transformer_factory tag for autowiring. Then it will be possible to use your own PropertyTransformer and add a parameter to the transform() call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants