diff --git a/extra/EMongoPartialDocument.php b/extra/EMongoPartialDocument.php index 8fa7cd0c..bbc5cce9 100644 --- a/extra/EMongoPartialDocument.php +++ b/extra/EMongoPartialDocument.php @@ -42,7 +42,7 @@ public function isPartial() */ public function getLoadedFields() { - return $this->_loadedFields; + return $this->_partial ? $this->_loadedFields : array(); } /** @@ -52,10 +52,10 @@ public function getLoadedFields() */ public function getUnloadedFields() { - return array_diff( + return $this->_partial ? array_diff( $this->_loadedFields, $this->attributeNames() - ); + ) : array(); } /** @@ -75,6 +75,28 @@ public function __get($name) return parent::__get($name); } + /** + * If user explicitly sets the unloaded embedded field, consider it as an loaded one, if model is partially loaded + * @see EMongoEmbeddedDocument::__set() + */ + public function __set($name, $value) + { + $return = parent::__set($name, $value); + + if($this->_partial && !in_array($name, $this->_loadedFields)) + { + $this->_loadedFields[] = $name; + + if(count($this->_loadedFields) === count($this->attributeNames())) + { + $this->_partial = false; + $this->loadedFields = null; + } + } + + return $return; + } + /** * Loads additional, previously unloaded attributes * to this document.