diff --git a/lib/Doctrine/EntityTraits/SafeToOneAssociationLoadTrait.php b/lib/Doctrine/EntityTraits/SafeToOneAssociationLoadTrait.php new file mode 100644 index 0000000000..1b224505e9 --- /dev/null +++ b/lib/Doctrine/EntityTraits/SafeToOneAssociationLoadTrait.php @@ -0,0 +1,30 @@ +$propertyName instanceof Proxy) { + return; + } + + if ($this->$propertyName->__isInitialized()) { + return; + } + + // if a proxy exists (= we have related entity ID), try to load it + // to see if it is a valid ID referencing an existing entity + try { + $this->$propertyName->__load(); + } catch (EntityNotFoundException $e) { + $this->$propertyName = $emptyValue; + } + } +}