Skip to content

Commit

Permalink
Better partial embedded fields handling
Browse files Browse the repository at this point in the history
  • Loading branch information
canni committed Jan 26, 2011
1 parent faa1702 commit 3f390f8
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions extra/EMongoPartialDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function isPartial()
*/
public function getLoadedFields()
{
return $this->_loadedFields;
return $this->_partial ? $this->_loadedFields : array();
}

/**
Expand All @@ -51,10 +51,10 @@ public function getLoadedFields()
*/
public function getUnloadedFields()
{
return array_diff(
return $this->_partial ? array_diff(
$this->_loadedFields,
$this->attributeNames()
);
) : array();
}

/**
Expand All @@ -74,6 +74,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.
Expand Down

0 comments on commit 3f390f8

Please sign in to comment.