douqin1932 2013-03-22 23:36
浏览 31
已采纳

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

  • 写回答

3条回答 默认 最新

  • dtuzjzs3853 2013-04-04 00:24
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名