Skip to content

Commit

Permalink
A first approach to separating hydration of associations from the mai…
Browse files Browse the repository at this point in the history
…n loop

This will make the associations hydration more robust
  • Loading branch information
lorenzo committed Dec 15, 2014
1 parent a13caea commit 1e6936f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/ORM/EagerLoader.php
Expand Up @@ -510,7 +510,7 @@ public function associationsMap($table) {

$visitor = function ($level, $matching = false) use (&$visitor, &$map) {
foreach ($level as $assoc => $meta) {
$map[$meta['aliasPath']] = [
$map[] = [
'alias' => $assoc,
'instance' => $meta['instance'],
'canBeJoined' => $meta['canBeJoined'],
Expand Down
49 changes: 34 additions & 15 deletions src/ORM/ResultSet.php
Expand Up @@ -328,11 +328,43 @@ protected function _fetchResult() {
protected function _groupResult($row) {
$defaultAlias = $this->_defaultTable->alias();
$results = $presentAliases = [];
$options = [
'useSetters' => false,
'markClean' => true,
'markNew' => false,
'guard' => false
];

foreach (collection($this->_associationMap)->match(['matching' => true]) as $matching) {
foreach ($row as $key => $value) {
if (strpos($key, $matching['alias'] . '__') !== 0) {
continue;
}
list($table, $field) = explode('__', $key);
$results['_matchingData'][$table][$field] = $value;
unset($row[$key]);
}
if (empty($results['_matchingData'][$matching['alias']])) {
continue;
}

$results['_matchingData'][$matching['alias']] = $this->_castValues(
$matching['instance']->target(),
$results['_matchingData'][$matching['alias']]
);

if ($this->_hydrate) {
$entity = new $matching['entityClass']($results['_matchingData'][$matching['alias']], $options);
$entity->clean();
$results['_matchingData'][$matching['alias']] = $entity;
}
}

foreach ($row as $key => $value) {
$table = $defaultAlias;
$field = $key;

if (isset($this->_associationMap[$key])) {
if (!is_scalar($value)) {
$results[$key] = $value;
continue;
}
Expand Down Expand Up @@ -360,14 +392,7 @@ protected function _groupResult($row) {
}
unset($presentAliases[$defaultAlias]);

$options = [
'useSetters' => false,
'markClean' => true,
'markNew' => false,
'guard' => false
];

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

Expand Down Expand Up @@ -402,12 +427,6 @@ protected function _groupResult($row) {
$results[$alias] = $entity;
}

if ($assoc['matching']) {
$results['_matchingData'][$alias] = $results[$alias];
unset($results[$alias]);
continue;
}

$results = $instance->transformRow($results, $alias, $assoc['canBeJoined']);
}

Expand Down

0 comments on commit 1e6936f

Please sign in to comment.