Skip to content

Commit 9a42530

Browse files
committed
minor #11067 Update "Ignoring Attributes" (brucep)
This PR was squashed before being merged into the 4.2 branch (closes #11067). Discussion ---------- Update "Ignoring Attributes" The `setIgnoredAttributes()` method has been [deprecated](https://github.com/symfony/serializer/blob/4.2/Normalizer/AbstractNormalizer.php#L181). Commits ------- 579ad57 Update \"Ignoring Attributes\"
2 parents 45cb5f2 + 579ad57 commit 9a42530

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

components/serializer.rst

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,24 +401,30 @@ Ignoring Attributes
401401

402402
.. note::
403403

404-
Using attribute groups instead of the :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes`
405-
method is considered best practice.
404+
Using :ref:`attribute groups <component-serializer-attributes-groups>` is considered best practice.
406405

407-
As an option, there's a way to ignore attributes from the origin object. To remove
408-
those attributes use the
409-
:method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes`
410-
method on the normalizer definition::
406+
As an option, there's a way to ignore attributes from the origin object.
407+
To remove those attributes provide an array via the ``ignored_attributes``
408+
key in the ``context`` parameter of the desired serializer method::
411409

410+
use Acme\Person;
412411
use Symfony\Component\Serializer\Serializer;
413412
use Symfony\Component\Serializer\Encoder\JsonEncoder;
414413
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
415414

415+
$person = new Person();
416+
$person->setName('foo');
417+
$person->setAge(99);
418+
416419
$normalizer = new ObjectNormalizer();
417-
$normalizer->setIgnoredAttributes(['age']);
418420
$encoder = new JsonEncoder();
419421

420422
$serializer = new Serializer([$normalizer], [$encoder]);
421-
$serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsperson":false}
423+
$serializer->serialize($person, 'json', ['ignored_attributes' => 'age']); // Output: {"name":"foo"}
424+
425+
.. deprecated:: 4.2
426+
427+
The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes` method was deprecated in Symfony 4.2.
422428

423429
.. _component-serializer-converting-property-names-when-serializing-and-deserializing:
424430

0 commit comments

Comments
 (0)