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

Adding a transformer to an array of objects #111

Closed
pedrem opened this issue Apr 16, 2024 · 9 comments · Fixed by #116
Closed

Adding a transformer to an array of objects #111

pedrem opened this issue Apr 16, 2024 · 9 comments · Fixed by #116

Comments

@pedrem
Copy link

pedrem commented Apr 16, 2024

Hello,

I'm trying to transform an array of objects (DTOs) into an array(entities), Is that something that the library is capable of handling?

Here's a snippet of the code:

/** @var ColourApi[] */
#[MapTo(transformer: ColourTransformer::class)]
public array $colours = [];

When looking at the automapper tab in the profiler although it attaches AutoMapper\Transformer\PropertyTransformer\PropertyTransformer there's no code inside.

I've the same setup for simple properties, so I know the problem is probably not the transformer.

Let me know if you need more context.

Thank you!

@joelwurtz
Copy link
Member

joelwurtz commented Apr 17, 2024

It should not be a problem normally, we may need more context or a simple reproducer so we can see what's the problem here

@pedrem
Copy link
Author

pedrem commented Apr 17, 2024

I'll try to provide a little more context:

So here's a snippet of the entity which I'm trying to map to:

/** @var Colour[] */
#[ORM\ManyToMany(targetEntity: Colour::class)]
#[ORM\JoinTable(name: 'campaignProductColours')]
#[ORM\JoinColumn(name: 'campaignProductId', referencedColumnName: 'campaignProductId')]
private Collection $colours;

Without adding a custom transformer it uses ArrayTransformer, but once I add the transformer(1st snippet) no code is actually attached, see example image below:

No custom transformer on colours property
Screenshot 2024-04-17 at 09 47 29

Custom transformer on colours property
Screenshot 2024-04-17 at 09 45 32

I've tried both these approaches but same result:

Bottom line I'm trying to hydrate the entities using the transformer, so please let me know if there's a way without using transformers!

@joelwurtz
Copy link
Member

Does the ColourTransformer is registered as a service (under the id ColourTransformer::class ?

@joelwurtz
Copy link
Member

Bottom line I'm trying to hydrate the entities using the transformer, so please let me know if there's a way without using transformers!

Yes you could use a provider see https://automapper.jolicode.com/dev/mapping/provider/

@pedrem
Copy link
Author

pedrem commented Apr 17, 2024

Does the ColourTransformer is registered as a service (under the id ColourTransformer::class ?

Yeah, you can see on the 2nd screenshot it correctly attaches to colourFeatured.

Not really sure this is the "problem" but I've been trying to understand why the code is not getting attached and found that it might be related to this.

@joelwurtz
Copy link
Member

joelwurtz commented Apr 17, 2024

Not exactly but something close to this: this may be because there is a transformer on a collection but the hydrate part can only be done with add / remove method.

I will try to look at that, can you add a 'setColours' on the target to see if it works ? (or make the property public)

@pedrem
Copy link
Author

pedrem commented Apr 17, 2024

I will try to look at that, can you add a 'setColours' on the target to see if it works ? (or make the property public)

Didn't seem to make a difference

@pedrem
Copy link
Author

pedrem commented Apr 17, 2024

No luck with Providers, are you able to share a working example?

@joelwurtz
Copy link
Member

Yeah, for the record, provider was badly injected in the bundle (should be fixed in next version).

For adder and remove i made #116 which should resolve your first issue.

For the record, it will transform the array entirely, transformer will not be applied to the item (so it need to handle an array as an input and return an array). Then it will correctly used the adder method

I think it's better than the item, as it will allow, in your case, to fetch a collection instead of doing a sql query for each item, also this is consistent with other way to write the values.

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

Successfully merging a pull request may close this issue.

2 participants