Skip to content

Commit d05e4d7

Browse files
committed
minor #13432 Added an example Entity for Serializer (TheGarious)
This PR was submitted for the 5.0 branch but it was squashed and merged into the 3.4 branch instead. Discussion ---------- Added an example Entity for Serializer To include an annotation Group for Serializer. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/roadmap for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `master` for features of unreleased versions). --> Commits ------- 9a13cd6 Added an example Entity for Serializer
2 parents 15330f4 + 9a13cd6 commit d05e4d7

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)