Skip to content

Added an example Entity for Serializer #13432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,45 @@ with the following configuration:
]);

Next, add the :ref:`@Groups annotations <component-serializer-attributes-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
Expand Down