Symfony2 Doctrine对实体负载的更改值

I have a database structure which has a field content. In the database this is a BLOB (so a string). content contains a JSON-encoded string.

When I load this, I would like to load it in to a specific Content object (with a different Content subclass for each possible format of the JSON).

Are there any events or anything which I can hook up so I can catch the value right before the Entity is built (so I can have setContent() type-hint the Content class instead of having to be generic, which I'd have to do if the information is loaded in to the Entity before I intercept).

Any ideas?


A bit more details. Basically what I imagine is having some sort of Factory class which takes a JSON string and converts it to a proper object.

{
    body: "ABC",
    value: 5
}

Goes to an object of a class like this:

class MyContent extends Content
{
    protected $body;
    protected $value;
}

I can't use typical object mapping because it's a JSON string to an object.


Further clarification

Basically, I have an entity named Box. Box has a content value, which is supposed to be an instance of Content.

Under normal circumstances, if Content was a normal database Entity, I would just hook up a One-to-One relationship between Box and Content, which would load Content properly in to the Box without needing to do anything special.

However, in this case, Content can have many forms. To handle this, it is stored as a JSON object in a BLOB field in Box's table. This means when Doctrine tries to load Box, it will try to load a string.

I could simply have Box::setContent() accept any parameter and deal with it accordingly based on if it is a string or Content object.

However, I'd like it so when it is used it is always a Content object, so I want type-hinting for the function (i.e., Box::setContent(Content $content)). The problem is this would prevent Doctrine from giving that field a string.

Which is why I want to intercept the value Doctrine has for content and replace it with a proper object before it loads it in to the entity Box.

I don't think any of the Doctrine events do exactly what I want, so it may not really be possible. =S

Ultimately what I ended up doing was just creating different tables and Entities for each possible Content type and then using Doctrine's inheritence to let Doctrine handle them. Not idea (since there may be many, many types which means more tables and I can't just dynamically constructor new types), but it works well for now.

Doctrine Inheritence

You should be able to add code to the entities get method to do this. You can also check out the lifecycle call backs for doctrine: http://docs.doctrine-project.org/en/2.0.x/reference/events.html#lifecycle-callbacks

You can use JMSSerializer to create entities from JSON - https://github.com/schmittjoh/JMSSerializerBundle.

And I can`t understand you question, but maybe you can use json_array type - http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#doctrine-mapping-types

You need to load data from DB and than create entities? Or load entities with some field that have nested entity deserialized from JSON?