Skip to content

Commit faf510b

Browse files
author
Gary Houbre
authored
Added an example
To include an annotation Group for Serializer.
1 parent 9e329be commit faf510b

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

serializer.rst

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,53 @@ To use annotations, first add support for them via the SensioFrameworkExtraBundl
138138
$ composer require sensio/framework-extra-bundle
139139
140140
Next, add the :ref:`@Groups annotations <component-serializer-attributes-groups-annotations>`
141-
to your class and choose which groups to use when serializing::
141+
to your class::
142+
143+
// src/Entity/Product.php
144+
namespace App\Entity;
145+
146+
use Doctrine\ORM\Mapping as ORM;
147+
use Symfony\Component\Serializer\Annotation\Groups;
148+
149+
/**
150+
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
151+
*/
152+
class Product
153+
{
154+
155+
/**
156+
* @ORM\Id
157+
* @ORM\GeneratedValue
158+
* @ORM\Column(type="integer")
159+
* @Groups({"show_product", "list_product"})
160+
*/
161+
private $id;
162+
163+
/**
164+
* @ORM\Column(type="string", length=255)
165+
* @Groups({"show_product", "list_product"})
166+
*/
167+
private $name;
168+
169+
/**
170+
* @ORM\Column(type="integer")
171+
* @Groups({"show_product", "list_product"})
172+
*/
173+
private $price;
174+
175+
/**
176+
* @ORM\Column(type="integer")
177+
* @Groups({"show_product"})
178+
*/
179+
private $description;
180+
181+
}
182+
183+
and choose which groups to use when serializing::
142184

143185
$json = $serializer->serialize(
144-
$someObject,
145-
'json', ['groups' => 'group1']
186+
$product,
187+
'json', ['groups' => 'show_product']
146188
);
147189

148190
.. tip::

0 commit comments

Comments
 (0)