Maarten de Boer
Cloudstek
Published in
1 min readMay 25, 2019

--

Well at last my response, but it is quite simple. You create a normalizer with a high priority. In the `supportsDenormalization` method you check if `$type` matches the item you want to deserialize (e.g. `strpos($type, ‘App\\Entity\\’) === 0`) and check if `$data` is an ID. Because you use the data argument, the normalizer isn’t cacheable.

Then in `denormalize` do whatever you do to find your entity and return it.

You’re free to implemented however you like. If in `supportsDenormalization` you only allow integers and not arrays, it is smart enough to still support arrays of IDs when it expects an array of your entities (e.g. a many-to-many). But of course you can change it to accept arrays directly which saves a few calls to your normalizer, improving performance.

--

--