vendredi 24 août 2018

How to serialize objects so their nested entities are represented by a id?

Let's say there are objects A, B and C. They're all Doctrine ORM entities.

Object A is the aggregate root - it's a composite of B and C objects. Serializing object A returns it's own data and also properties of B and C. This is something I want to change in the most graceful way possible.

Object A should serialize in a way that retains it's properties but represents B and C with one of their properties - in this case their id. JSON example:

{
  "objA": {
    "property": true,
    "objB": 10,
    "objC": "a1c81b5a-6ecd-4c40-a541-f779b907418c",
  }
}

I was wondering what is a good way that could be used system-wide to achieve that.

This is how I want to represent nested entities in almost every situation in my application - someone suggested me to implement Presentation Models or ViewModels for every case, but isn't there a way to configure this in Doctrine or Symfony's serializer?

Aucun commentaire:

Enregistrer un commentaire