4

I acknowledge of the existence of $normalizer->setIgnoredAttributes but I have the following problem.

I have an entity Product with attributes 'prices' (related with another entity) and 'complements' (which is a self reference relation). When I get the a product I need the prices, but when listing the complements, I don't need the prices in the complement product, is there any way to avoid getting the attribute prices only in the complements? Something like

$normalizer->setIgnoredAttributes(array('complement->prices')); 
1
  • 2
    did you try attribute groups? According to the symfony docs it is the preferred method to ignore attributes: symfony.com/doc/current/components/… Commented Apr 14, 2016 at 10:28

2 Answers 2

12

There are several ways to accomplish this:

  1. Use the Serializer annotations and specify different Serialization groups
  2. Use the CustomNormalizer and make your Product implement the NormalizableInterface
  3. Write a custom normalizer class which only supports you Product entity.

The Serialization groups

By using annotations on each property of the Product entity you can specify if that property should be serialized or not, if it needs to be aliased or not, or if it belongs to one or more groups.

When serializing you can then specify via the $context array which serialization group you want to serialize and the serializer will only serialize members of that group.

The NormalizableInterface

By implementing NormalizableInterface in your Product entity you are passing the responsibility of normalization onto the entity itself. It decides what the final normalized product will look like.

By passing some information/flags in the $context array you ensure the normalization logic of the product entity will be aware if it's currently normalizing a standard product or a complement.

The custom normalizer class

Without having to implement the NormalizableInterface on the entity your new normalizer class will only accept normalizing your Product entity (or whatever you decide to specify in supportsNormalization).

The same $context logic must apply here as with the previous example.

Sign up to request clarification or add additional context in comments.

Comments

3

Should you ever need to completely exclude a property from being serialized, then since Symfony version 5.1 you have a much simpler option: the @Ignore annotation:

For example:

use Symfony\Component\Serializer\Annotation\Ignore; class User { public $login; public $email; /** * @Ignore */ public $password; } 

And it works on access methods too:

class User { // ... /** * @Ignore */ public function getPassword(): string {} /** * @Ignore */ public function isAdmin(): bool {} } 

4 Comments

I don't think that this works - the requirement is that prices are serialized when serializing the product entity itself, but not when that entity is serialized through that "complements" relation
@NicoHaase you are right, I've updated the answer to be more explicit what exactly can be achieved with this solution.
It seems to still be serialized in my install of Symfony 5.1
it doesn't work for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.