Skip to content

Commit 494c034

Browse files
[Serializer] Groups annotation/attribute on class
1 parent 8e87d62 commit 494c034

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

serializer.rst

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ Using Serialization Groups Attributes
295295
-------------------------------------
296296

297297
You can add :ref:`#[Groups] attributes <component-serializer-attributes-groups-attributes>`
298-
to your class::
298+
to your class properties::
299299

300300
// src/Entity/Product.php
301301
namespace App\Entity;
@@ -321,7 +321,37 @@ to your class::
321321
private string $description;
322322
}
323323

324-
You can now choose which groups to use when serializing::
324+
You can also use the ``#[Groups]`` attribute on class level::
325+
326+
#[ORM\Entity]
327+
#[Groups(['show_product'])]
328+
class Product
329+
{
330+
#[ORM\Id]
331+
#[ORM\GeneratedValue]
332+
#[ORM\Column(type: 'integer')]
333+
#[Groups(['list_product'])]
334+
private int $id;
335+
336+
#[ORM\Column(type: 'string', length: 255)]
337+
#[Groups(['list_product'])]
338+
private string $name;
339+
340+
#[ORM\Column(type: 'text')]
341+
private string $description;
342+
}
343+
344+
In this example, the ``id`` and the ``name`` properties belong to the
345+
``show_product`` and ``list_product`` groups. The ``description`` property
346+
only belongs to the ``show_product`` group.
347+
348+
.. versionadded:: 6.4
349+
350+
The support of the ``#[Groups]`` attribute on class level was
351+
introduced in Symfony 6.4.
352+
353+
Now that your groups are defined, you can choose which groups to use when
354+
serializing::
325355

326356
use Symfony\Component\Serializer\Context\Normalizer\ObjectNormalizerContextBuilder;
327357

0 commit comments

Comments
 (0)