test persist an uninitialized lazy ghost#12297
Conversation
|
I've spoken to @soyuka about this issue. The issue is that app tries to persist lazy objects as new records. Those are lazy objects that are not Doctrine proxies. Because we read the object's values through reflection, we don't initialize the objects and basically read How shall we handle this? Should we attempt to detect lazy objects and initialize them? |
|
I'm sorry but I don't think I get it.
So you're receiving JSON from an http request and stream that data into several entities, not all of which you persist, hence why you can't afford to initialize them? |
|
I can initialize it but I was wondering if Doctrine should initialize it itself if its persisted, as right now if you attempt to persist a lazy ghost entity (that wasn't created by Doctrine) then it fails and never initializes it (as doctrine uses reflection). |
|
I think there are at least 2 point of views: a) If we pass an entity to the ORM, it should be able to deal with it regardless of whether it's lazy or not. It shouldn't matter that the object is lazy. Regardless of the answer, it seems that we should attempt to detect whether objects are lazy or not. Do we have a way to do that that is reliable and where performance won't be a concern? |
Yes, we can use reflection to detect a lazy object. And since we're already accessing the object's properties through reflection, one more check won't hurt, I guess. |
|
I think I lean more towards scenario b. What about you? |
|
If I pass an object to the ORM via the And usually, the laziness would be broken by reading the properties. It's more or less an accident that it does not happen in our case. |
|
True. So how do we trigger the initialization then? |
|
Something like this? if (\PHP_VERSION_ID > 80400) {
$r = new \ReflectionClass($value);
if ($r->isUninitializedLazyObject($value)) {
$r->initializeLazyObject($value);
}
} |
|
Looks good. Please push a commit with that. |
We've the use case inside API Platform where the JsonStreamer deserializes a JSON into a Lazy Ghost as while its not consumed (streaming) we don't need to initialize this object. Therefore at some point we try to persist an uninitialized lazy ghost and it'll likely throw "column title must not be null".