Skip to content

Commit 60c44f2

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: Minor: Add linebreak. refs #13432 Added an example Entity for Serializer
2 parents c65140e + 3514fbf commit 60c44f2

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

serializer.rst

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,46 @@ 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()
151+
*/
152+
class Product
153+
{
154+
/**
155+
* @ORM\Id
156+
* @ORM\GeneratedValue
157+
* @ORM\Column(type="integer")
158+
* @Groups({"show_product", "list_product"})
159+
*/
160+
private $id;
161+
162+
/**
163+
* @ORM\Column(type="string", length=255)
164+
* @Groups({"show_product", "list_product"})
165+
*/
166+
private $name;
167+
168+
/**
169+
* @ORM\Column(type="integer")
170+
* @Groups({"show_product"})
171+
*/
172+
private $description;
173+
}
174+
175+
You can now choose which groups to use when serializing::
142176

143177
$json = $serializer->serialize(
144-
$someObject,
145-
'json', ['groups' => 'group1']
178+
$product,
179+
'json',
180+
['groups' => 'show_product']
146181
);
147182

148183
.. tip::

0 commit comments

Comments
 (0)