# Serialization Groups

Serialization groups are a feature of Symfony (and the JMS Serializer library), which can be used to limit which properties of an entity are returned in an API response (or anywhere that an entity is serialized). This allows the same entity to be serialized in different ways for different endpoints or for different users of the same endpoint. For example, an administrator user might be able to see more properties of an entity than a guest user.

If you are using serialization groups to limit what gets output by your API endpoint, there is no point in loading values from the database for properties that you are never going to access. You can therefore tell Objectiphy about the groups that you are going to serialize, and it will then limit the hydration of values to just those that belong to the specified groups. This can have a very beneficial impact on performance.

Objectiphy supports Symfony groups and JMS Serializer groups, and also has a 'dummy' group annotation of its own, which you can use to limit hydration of properties even if you are not using a serializer (in fact, any annotation which has a `groups` property or a `getGroups` method could be used here). Note, if you use the Objectiphy `@Group` annotation, it is not really a 'serialization group' because it doesn't affect serialization at all - it is just a 'hydration group'.

On your entity, you apply the required group(s) to your entity properties - for example:

```php
namespace MyNamespace\Entity;

use Symfony\Component\Serializer\Annotation\Groups;
use Objectiphy\Objectiphy\Mapping\Column;

#[Table(name: 'product')]
class Product
{
    #[Groups(['Default'])]
    #[Column(isPrimaryKey: true)]
    public int $id;
    
    #[Groups(['Default'])]
    #[Column]
    public string $name;
    
    #[Column]
    public string $description;
    
    #[Groups(['Admin'])]
    #[Column]
    public float $netPrice;
    
    #[Groups(['Admin'])]
    #[Column]
    public float $commission;
    
    #[Groups(['Admin', 'Finance'])]
    public float $vat;
    
    #[Groups(['Default'])]
    #[Column]
    public float $totalPrice;
}
```

Here we have an entity with three different serialization groups specified (Default, Admin, and Finance). By specifying any combination of groups when serializing, you can control which properties are output. There is also a property there (`description`) which does not belong to any group. Of course, if you ask Objectiphy to find an instance of this entity, it will not know which groups you intend to serialize, so will just hydrate (ie. load values from the database and populate the properties of) all of them.&#x20;

To tell Objectiphy which groups you are using, set the serialization groups [config option](/configuration-options.md) before you load the entity, like this:

```php
$repository->setConfigOption(
    ConfigOptions::SERIALIZATION_GROUPS, 
    ['Default', 'Finance']
);
```

So in the above example, only the values for properties belonging to the `Default` and `Finance` groups will be loaded from the database. By default, any ungrouped properties (in this case, the `description` property), will be loaded. If you want to exclude ungrouped properties, you can set another config option like this:

```php
$repository->setConfigOption(
    ConfigOptions::HYDRATE_UNGROUPED_PROPERTIES, 
    false
);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.objectiphy.net/serialization-groups.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
