From 579ad57533904dd9cb66c126e2b2987f46469c0f Mon Sep 17 00:00:00 2001 From: Bruce Phillips Date: Wed, 27 Feb 2019 19:23:26 -0500 Subject: [PATCH] Update \"Ignoring Attributes\" --- components/serializer.rst | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index a7c2b00c221..1db451b7e78 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -394,24 +394,30 @@ Ignoring Attributes .. note:: - Using attribute groups instead of the :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes` - method is considered best practice. + Using :ref:`attribute groups ` is considered best practice. -As an option, there's a way to ignore attributes from the origin object. To remove -those attributes use the -:method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes` -method on the normalizer definition:: +As an option, there's a way to ignore attributes from the origin object. +To remove those attributes provide an array via the ``ignored_attributes`` +key in the ``context`` parameter of the desired serializer method:: + use Acme\Person; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; + $person = new Person(); + $person->setName('foo'); + $person->setAge(99); + $normalizer = new ObjectNormalizer(); - $normalizer->setIgnoredAttributes(['age']); $encoder = new JsonEncoder(); $serializer = new Serializer([$normalizer], [$encoder]); - $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsperson":false} + $serializer->serialize($person, 'json', ['ignored_attributes' => 'age']); // Output: {"name":"foo"} + +.. deprecated:: 4.2 + + The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes` method was deprecated in Symfony 4.2. .. _component-serializer-converting-property-names-when-serializing-and-deserializing: