Skip to content

Commit

Permalink
Implementing the logic in the Association class to create an empty
Browse files Browse the repository at this point in the history
association value
  • Loading branch information
lorenzo committed Apr 10, 2014
1 parent 258b201 commit 8b506f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/ORM/Association.php
Expand Up @@ -463,6 +463,24 @@ public function transformRow($row, $nestKey, $joined) {
return $row;
}

/**
* Returns a modified row after appending a property for this association
* with the default empty value according to whether the association was
* joined or fetched externally.
*
* @param array $row
* @param boolean $joined Whether or not the row is a result of a direct join
* with this association
* @return array
*/
public function defaultRowValue($row, $joined) {
$sourceAlias = $this->source()->alias();
if (isset($row[$sourceAlias])) {
$row[$sourceAlias][$this->property()] = $joined ? null : [];
}
return $row;
}

/**
* Proxies the finding operation to the target table's find method
* and modifies the query accordingly based of this association
Expand Down
4 changes: 3 additions & 1 deletion src/ORM/ResultSet.php
Expand Up @@ -387,11 +387,13 @@ protected function _groupResult($row) {

foreach (array_reverse($this->_associationMap) as $assoc) {
$alias = $assoc['nestKey'];
$instance = $assoc['instance'];

if (!isset($results[$alias])) {
$results = $instance->defaultRowValue($results, $assoc['canBeJoined']);
continue;
}

$instance = $assoc['instance'];
$target = $instance->target();
$options['source'] = $target->alias();
unset($presentAliases[$alias]);
Expand Down

0 comments on commit 8b506f3

Please sign in to comment.