简体   繁体   English

实体负载上的Symfony2原则更改值

[英]Symfony2 Doctrine Change Value on Entity Load

I have a database structure which has a field content . 我有一个具有字段content的数据库结构。 In the database this is a BLOB (so a string ). 在数据库中,这是一个BLOB (所以是一个string )。 content contains a JSON-encoded string. content包含JSON编码的字符串。

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). 加载此文件时,我想将其加载到特定的Content对象(每种可能的JSON格式具有不同的Content子类)。

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). 是否有任何事件或任何我可以关联的事件,以便我可以在构建实体之前就捕获值(因此,我可以使用setContent()类型提示Content类,而不必是通用类,而我必须这样做如果我在拦截之前将信息加载到Entity中,则执行该操作)。

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. 基本上我想像的是具有某种Factory类,该类接受JSON字符串并将其转换为适当的对象。

{
    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. 我无法使用典型的对象映射,因为它是到对象的JSON字符串。


Further clarification 进一步澄清

Basically, I have an entity named Box . 基本上,我有一个名为Box的实体。 Box has a content value, which is supposed to be an instance of Content . Box具有一个content值,该值应该是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. 在正常情况下,如果Content是普通的数据库实体,则只需在BoxContent之间建立一对一关系,即可将Content正确地加载到Box而无需执行任何特殊操作。

However, in this case, Content can have many forms. 但是,在这种情况下, Content可以具有多种形式。 To handle this, it is stored as a JSON object in a BLOB field in Box 's table. 为了解决这个问题,它作为JSON对象存储在Box表的BLOB字段中。 This means when Doctrine tries to load Box , it will try to load a string. 这意味着当Doctrine尝试加载Box ,它将尝试加载字符串。

I could simply have Box::setContent() accept any parameter and deal with it accordingly based on if it is a string or Content object. 我可以简单地让Box::setContent()接受任何参数,并根据它是字符串还是Content对象进行相应的处理。

However, I'd like it so when it is used it is always a Content object, so I want type-hinting for the function (ie, Box::setContent(Content $content) ). 但是,我希望它在使用时始终是一个Content对象,因此我想对该函数进行类型提示(即Box::setContent(Content $content) )。 The problem is this would prevent Doctrine from giving that field a string. 问题在于,这将阻止Doctrine向该字段提供字符串。

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 . 这就是为什么我要截获Doctrine的content值并将其加载到实体Box之前用适当的对象替换它。

I don't think any of the Doctrine events do exactly what I want, so it may not really be possible. 我认为没有任何教义事件能够完全满足我的要求,因此可能实际上是不可能的。 =S = S

You should be able to add code to the entities get method to do this. 您应该能够将代码添加到实体的get方法中以执行此操作。 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 您还可以查看该准则的生命周期回调: 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 . 您可以使用JMSSerializer从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 我不能老是理解你的问题,但也许你可以使用json_array类型- http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#doctrine-mapping类型

You need to load data from DB and than create entities? 您需要从数据库加载数据,而不是创建实体? Or load entities with some field that have nested entity deserialized from JSON? 还是加载具有从JSON反序列化的嵌套实体的某些字段的实体?

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. 最终,我要做的就是为每种可能的Content类型创建不同的表和实体,然后使用Doctrine的继承来让Doctrine处理它们。 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 教义继承

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM