Skip to content

Commit

Permalink
Custom setattribute method for EMongoEmbeddedDocument
Browse files Browse the repository at this point in the history
The safeOnly value has not been passed to subdocuments, and this caused
that attributes not explicitly marked as safe were not loaded with the
parent document.
  • Loading branch information
Nagy Attila Gabor committed Dec 21, 2010
1 parent 5ba6c72 commit 0578e5d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions EMongoEmbeddedDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,34 @@ public function __set($name, $value)
else
parent::__set($name, $value);
}

/**
* Sets the attribute values in a massive way.
* @param array $values attribute values (name=>value) to be set.
* @param boolean $safeOnly whether the assignments should only be done to the safe attributes.
* A safe attribute is one that is associated with a validation rule in the current {@link scenario}.
* @see getSafeAttributeNames
* @see attributeNames
*/
public function setAttributes($values,$safeOnly=true)
{
if(!is_array($values))
return;
$attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());
$hasEmbedded = $this->hasEmbeddedDocuments();
foreach($values as $name=>$value)
{
if(isset($attributes[$name])) {
if ($hasEmbedded && $this->_embedded->contains($name)) {
$this->$name->setAttributes($value, $safeOnly);
} else {
$this->$name=$value;
}
}
else if($safeOnly)
$this->onUnsafeAttribute($name,$value);
}
}

public function afterValidate()
{
Expand Down

0 comments on commit 0578e5d

Please sign in to comment.