Search code examples
entitysymfony-3.2

Symfony 3 advice : retrieve data from another entity


I am new to Symfony and I was wondering how I would achieve this : I'm retrieving data from one entity, and one field is in JSON. It references data from other entities that I would like to convert into objects. Here is an example :

// JSON received
{items:"221,223",level:1,places:"12,15,17"}

I want this to be turned into :

Array(
    'items' => array(
        0 => Object(MyBundle/Entity/Item),
        1 => Object(MyBundle/Entity/Item)
    ),
    'level' => 1,
    'places' => array(
        0 => Object(MyBundle/Entity/Place),
        1 => Object(MyBundle/Entity/Place)
    ),
)

What would be the best practice way of retrieving these Item objects as calling an entity from another entity is not a good idea ?

I thought of serializing them as the embedded objects will be rather small, but is it the best solution ?

The JSON could contain many other entities, or none, the content is not fixed.


Solution

  • I ended up serializing my objects and saving them in the database. The whole process is managed by Symfony itself, as the field in the table has been declared as object.

    I don't know if this is the preferred way of doing it but it perfectly fits my needs, and AFAIK it doesn't break any logic.

    Moreover the objects that are serialized are really small so for the moment I don't see any drawback.