Skip to content

Commit 9a13cd6

Browse files
Gary HoubreOskarStark
Gary Houbre
authored andcommitted
Added an example Entity for Serializer
1 parent 15330f4 commit 9a13cd6

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

serializer.rst

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,45 @@ with the following configuration:
212212
]);
213213
214214
Next, add the :ref:`@Groups annotations <component-serializer-attributes-groups-annotations>`
215-
to your class and choose which groups to use when serializing::
215+
to your class::
216+
217+
// src/Entity/Product.php
218+
namespace App\Entity;
219+
220+
use Doctrine\ORM\Mapping as ORM;
221+
use Symfony\Component\Serializer\Annotation\Groups;
222+
223+
/**
224+
* @ORM\Entity()
225+
*/
226+
class Product
227+
{
228+
/**
229+
* @ORM\Id
230+
* @ORM\GeneratedValue
231+
* @ORM\Column(type="integer")
232+
* @Groups({"show_product", "list_product"})
233+
*/
234+
private $id;
235+
236+
/**
237+
* @ORM\Column(type="string", length=255)
238+
* @Groups({"show_product", "list_product"})
239+
*/
240+
private $name;
241+
242+
/**
243+
* @ORM\Column(type="integer")
244+
* @Groups({"show_product"})
245+
*/
246+
private $description;
247+
}
248+
249+
You can now choose which groups to use when serializing::
216250

217251
$json = $serializer->serialize(
218-
$someObject,
219-
'json', ['groups' => ['group1']]
252+
$product,
253+
'json', ['groups' => 'show_product']
220254
);
221255

222256
In addition to the ``@Groups`` annotation, the Serializer component also

0 commit comments

Comments
 (0)