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