Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 999d56f

Browse filesBrowse files
committed
feature #8830 [Serializer] Add docs for attributes context key (dunglas, javiereguiluz)
This PR was merged into the 3.3 branch. Discussion ---------- [Serializer] Add docs for attributes context key Documentation for symfony/symfony#18834 Commits ------- d48d6d4 Minor reword ea211ab Review 5d75696 [Serializer] Add docs for attributes context key
2 parents 663f818 + d48d6d4 commit 999d56f
Copy full SHA for 999d56f

File tree

1 file changed

+40
-0
lines changed
Filter options

1 file changed

+40
-0
lines changed

‎components/serializer.rst

Copy file name to clipboardExpand all lines: components/serializer.rst
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,46 @@ You are now able to serialize only attributes in the groups you want::
333333

334334
.. _ignoring-attributes-when-serializing:
335335

336+
Selecting Specific Attributes
337+
-----------------------------
338+
339+
It is also possible to serialize only a set of specific attributes::
340+
341+
use Symfony\Component\Serializer\Serializer;
342+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
343+
344+
class User
345+
{
346+
public $familyName;
347+
public $givenName;
348+
public $company;
349+
}
350+
351+
class Company
352+
{
353+
public $name;
354+
public $address;
355+
}
356+
357+
$company = new Company();
358+
$company->name = 'Les-Tilleuls.coop';
359+
$company->address = 'Lille, France';
360+
361+
$user = new User();
362+
$user->familyName = 'Dunglas';
363+
$user->givenName = 'Kévin';
364+
$user->company = $company;
365+
366+
$serializer = new Serializer(array(new ObjectNormalizer()));
367+
368+
$data = $serializer->normalize($user, null, array('attributes' => array('familyName', 'company' => ['name'])));
369+
// $data = array('familyName' => 'Dunglas', 'company' => array('name' => 'Les-Tilleuls.coop'));
370+
371+
Only attributes that are not ignored (see below) are available.
372+
If some serialization groups are set, only attributes allowed by those groups can be used.
373+
374+
As for groups, attributes can be selected during both the serialization and deserialization process.
375+
336376
Ignoring Attributes
337377
-------------------
338378

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.