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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀