From 9a13cd650b59092d0eaf1580aa8a5cebf43687da Mon Sep 17 00:00:00 2001 From: Gary Houbre Date: Sat, 28 Mar 2020 00:16:24 +0100 Subject: [PATCH] Added an example Entity for Serializer --- serializer.rst | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/serializer.rst b/serializer.rst index 0ee928b8139..110c2aa4f1d 100644 --- a/serializer.rst +++ b/serializer.rst @@ -212,11 +212,45 @@ with the following configuration: ]); Next, add the :ref:`@Groups annotations ` -to your class and choose which groups to use when serializing:: +to your class:: + + // src/Entity/Product.php + namespace App\Entity; + + use Doctrine\ORM\Mapping as ORM; + use Symfony\Component\Serializer\Annotation\Groups; + + /** + * @ORM\Entity() + */ + class Product + { + /** + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type="integer") + * @Groups({"show_product", "list_product"}) + */ + private $id; + + /** + * @ORM\Column(type="string", length=255) + * @Groups({"show_product", "list_product"}) + */ + private $name; + + /** + * @ORM\Column(type="integer") + * @Groups({"show_product"}) + */ + private $description; + } + +You can now choose which groups to use when serializing:: $json = $serializer->serialize( - $someObject, - 'json', ['groups' => ['group1']] + $product, + 'json', ['groups' => 'show_product'] ); In addition to the ``@Groups`` annotation, the Serializer component also