原则 2:实体可以保存到会话中吗?
我在将实体保存到 PHP 会话后遇到延迟加载问题.有什么解决方法吗?
I'm having a problem with lazy loading after I save an entity into a PHP session. Is there any workaround for this?
推荐答案
参见 序列化实体在教义手册中:(您在会话中保存的所有内容都被序列化和反序列化.)
See Serializing Entities in doctrine manual: (Everything you save in a session is serialized and deserialized.)
序列化实体可能有问题,并不真正推荐,至少只要实体实例仍然持有对代理对象的引用或仍然由 EntityManager 管理.
Serializing entities can be problematic and is not really recommended, at least not as long as an entity instance still holds references to proxy objects or is still managed by an EntityManager.
在代理实体(代理延迟加载的实体)时,有一个技术限制可以避免私有属性被序列化.
There is a technical limitation that avoid private properties from being serialized when an entity is proxied (lazy-loaded entities are proxied).
这意味着您必须避免对要序列化的实体使用 private
属性(改为使用 protected
实体).
This means that you have to avoid using private
properties for the entities you want to serialize (use protected
entities instead).
此外,如果延迟加载的实体在序列化时未加载,则在反序列化后将无法加载.所以你必须确保实体在序列化之前完全加载.
Also, if a lazy-loaded entity is not loaded at serialization time, it won't be loadable after de-serialization. So you have to make sure the entity is fully loaded before serializing it.
相关文章