Skip to content

Commit

Permalink
Document: to(array) now works recursively. Patch by Nate Abele
Browse files Browse the repository at this point in the history
Entity:The data method not uses ->to('array') instead of trying to do a straight dump and to
(array) now works recusively.
  • Loading branch information
Howard3 authored and nateabele committed Jun 11, 2011
1 parent a4a3883 commit d9ee039
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
13 changes: 9 additions & 4 deletions data/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ public function data($name = null) {
if ($name) {
return $this->__get($name);
}
$related = array_map(function($rel) { return $rel->data(); }, $this->_relationships);
return array_merge($this->_updated + $this->_data, $related);
return $this->to('array');
}

/**
Expand Down Expand Up @@ -377,9 +376,15 @@ public function export() {
* @return mixed
*/
public function to($format, array $options = array()) {
$map = function($obj) {
return $obj->data();
};

switch ($format) {
case 'array':
$result = Collection::toArray($this->data());
$data = $this->_updated + $this->_data;
$data = array_merge($data, array_map($map, $this->_relationships));
$result = Collection::toArray($data);
break;
default:
$result = $this;
Expand All @@ -389,4 +394,4 @@ public function to($format, array $options = array()) {
}
}

?>
?>
10 changes: 7 additions & 3 deletions data/entity/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,14 @@ public function to($format, array $options = array()) {
'MongoId' => function($value) { return (string) $value; },
'MongoDate' => function($value) { return $value->sec; }
));
$options += $defaults;

if ($format == 'array') {
$options += $defaults;
$data = array_merge($this->_data, $this->_updated);
$map = function($obj) {
return $obj->data();
};
$data = $this->_updated + $this->_data;
$data = array_merge($data, array_map($map, $this->_relationships));
return Collection::toArray(array_diff_key($data, $this->_removed), $options);
}
return parent::to($format, $options);
Expand Down Expand Up @@ -452,4 +456,4 @@ public function increment($field, $value = 1) {
}
}

?>
?>

0 comments on commit d9ee039

Please sign in to comment.