Skip to content

Commit 0b0472d

Browse files
committed
Merge branch '4.2'
* 4.2: Update \"Ignoring Attributes\"
2 parents 1b15af7 + 0d6c702 commit 0b0472d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

components/serializer.rst

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -399,26 +399,30 @@ As for groups, attributes can be selected during both the serialization and dese
399399
Ignoring Attributes
400400
-------------------
401401

402-
.. note::
403-
404-
Using attribute groups instead of the :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes`
405-
method is considered best practice.
406-
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::
402+
As an option, there's a way to ignore attributes from the origin object.
403+
To remove those attributes provide an array via the ``ignored_attributes``
404+
key in the ``context`` parameter of the desired serializer method::
411405

406+
use Acme\Person;
412407
use Symfony\Component\Serializer\Serializer;
413408
use Symfony\Component\Serializer\Encoder\JsonEncoder;
414409
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
415410

411+
$person = new Person();
412+
$person->setName('foo');
413+
$person->setAge(99);
414+
416415
$normalizer = new ObjectNormalizer();
417-
$normalizer->setIgnoredAttributes(['age']);
418416
$encoder = new JsonEncoder();
419417

420418
$serializer = new Serializer([$normalizer], [$encoder]);
421-
$serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsperson":false}
419+
$serializer->serialize($person, 'json', ['ignored_attributes' => 'age']); // Output: {"name":"foo"}
420+
421+
.. deprecated:: 4.2
422+
423+
The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes`
424+
method that was used as an alternative to the ``ignored_attributes`` option
425+
was deprecated in Symfony 4.2.
422426

423427
.. _component-serializer-converting-property-names-when-serializing-and-deserializing:
424428

0 commit comments

Comments
 (0)