Skip to content

Commit

Permalink
Merge branch 'entity-to-array' of https://github.com/oakey-b1/horde i…
Browse files Browse the repository at this point in the history
…nto oakey-b1-entity-to-array
  • Loading branch information
yunosh committed Sep 2, 2016
2 parents 23d7afd + 41c6275 commit 306c8be
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions framework/Rdo/lib/Horde/Rdo/Base.php
Expand Up @@ -446,4 +446,44 @@ protected function _fillPlaceholders($query)
return $query;
}

/**
* make the Entity convertible to an Array
* this method can be used when handing it over to Horde_Variables
* so that the database is not unnecessarily queried because of lazyFields/-Relationships
*
* @param bool $lazy if lazy elements should be added
* @param bool $relationships if relationships should be added
*
* @return array containing all selected fields / relationships
*/
public function toArray($lazy = false, $relationships = false)
{
$array = array();

$m = $this->getMapper();

foreach ($m->fields as $field) {
$array[$field] = $this->$field;
}

if ($lazy) {
foreach ($m->lazyFields as $field) {
$array[$field] = $this->$field;
}
}

if ($relationships) {
foreach ($m->relationships as $rel=>$field) {
$array[$rel] = $this->$rel;
}
}

if ($lazy && $relationships) {
foreach ($m->lazyRelationships as $rel=>$field) {
$array[$rel] = $this->$rel;
}
}

return $array;
}
}

0 comments on commit 306c8be

Please sign in to comment.