diff --git a/README.md b/README.md index 3362348..1858ae0 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,13 @@ using https://github.com/cweagans/composer-patches. You can review the contents of this patch in `patch/nullable_embeddables.patch` +It works by analyzing the types of the properties of the entity and the value-object. +If the entity property containing the value-object is declared as nullable and +none of the value-object's properties that are not declared nullable get null values from the +database - then the entity property will be hydrated with the value object. +All other cases are considered an invalid state based on the provided typing and will result in +hydrating to null on the corresponding property of the entity. + The tradeoffs of this approach will be: - You can't update Doctrine directly, only update it with this package - You will need PHP 7.4 diff --git a/patch/nullable_embeddables.patch b/patch/nullable_embeddables.patch index 49c7839..f3e2fee 100644 --- a/patch/nullable_embeddables.patch +++ b/patch/nullable_embeddables.patch @@ -1,13 +1,16 @@ diff --git a/lib/Doctrine/ORM/Mapping/ReflectionEmbeddedProperty.php b/lib/Doctrine/ORM/Mapping/ReflectionEmbeddedProperty.php -index b224fff4e..67d5e1da4 100644 +index b224fff4e..13404d91a 100644 --- a/lib/Doctrine/ORM/Mapping/ReflectionEmbeddedProperty.php +++ b/lib/Doctrine/ORM/Mapping/ReflectionEmbeddedProperty.php -@@ -72,13 +72,13 @@ class ReflectionEmbeddedProperty extends ReflectionProperty +@@ -72,13 +72,16 @@ class ReflectionEmbeddedProperty extends ReflectionProperty */ public function getValue($object = null) { - $embeddedObject = $this->parentProperty->getValue($object); -+ $embeddedObject = $this->parentProperty->isInitialized($object) ? $this->parentProperty->getValue($object) : null; ++ $embeddedObject = ! $this->parentProperty->getDeclaringClass()->isInstance($object) || ++ $this->parentProperty->isInitialized($object) ? ++ $this->parentProperty->getValue($object) : ++ null; if (null === $embeddedObject) { return null; @@ -18,7 +21,7 @@ index b224fff4e..67d5e1da4 100644 } /** -@@ -86,7 +86,18 @@ class ReflectionEmbeddedProperty extends ReflectionProperty +@@ -86,7 +89,21 @@ class ReflectionEmbeddedProperty extends ReflectionProperty */ public function setValue($object, $value = null) { @@ -34,7 +37,10 @@ index b224fff4e..67d5e1da4 100644 + return; + } + -+ $embeddedObject = $this->parentProperty->isInitialized($object) ? $this->parentProperty->getValue($object) : null; ++ $embeddedObject = ! $this->parentProperty->getDeclaringClass()->isInstance($object) || ++ $this->parentProperty->isInitialized($object) ? ++ $this->parentProperty->getValue($object) : ++ null; if (null === $embeddedObject) { $this->instantiator = $this->instantiator ?: new Instantiator();