Skip to content

Commit

Permalink
Some changes to make hasMAny eager loading work after making
Browse files Browse the repository at this point in the history
primaryKey() return array
  • Loading branch information
lorenzo committed Jan 8, 2014
1 parent 16b1902 commit 193951c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
15 changes: 9 additions & 6 deletions Cake/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -147,13 +147,16 @@ protected function _joinCondition(array $options) {
*/
protected function _resultInjector($fetchQuery, $resultMap) {
$source = $this->source();
$sourceKey = key($fetchQuery->aliasField(
$source->primaryKey(),
$source->alias()
));
$sAlias = $source->alias();
$tAlias = $this->target()->alias();

$sourceKeys = [];
foreach ($source->primaryKey() as $key) {
$sourceKeys[] = key($fetchQuery->aliasField($key, $sAlias));
}

$alias = $this->target()->alias();
$nestKey = $alias . '__' . $alias;
$sourceKey = implode(';', $sourceKeys);
$nestKey = $tAlias . '__' . $tAlias;
return function($row) use ($resultMap, $sourceKey, $nestKey) {
if (isset($resultMap[$row[$sourceKey]])) {
$row[$nestKey] = $resultMap[$row[$sourceKey]];
Expand Down
19 changes: 16 additions & 3 deletions Cake/ORM/Query.php
Expand Up @@ -1041,18 +1041,31 @@ protected function _collectKeys($statement) {
$source = $meta['instance']->source();
if ($meta['instance']->requiresKeys($meta['config'])) {
$alias = $source->alias();
$pkField = key($this->aliasField($source->primaryKey(), $alias));
$collectKeys[] = [$alias, $pkField];
$pkFields = [];
foreach($source->primaryKey() as $key) {
$pkFields[] = key($this->aliasField($key, $alias));
}
$collectKeys[] = [$alias, $pkFields, count($pkFields) === 1];
}
}

$keys = [];
if (!empty($collectKeys)) {
while ($result = $statement->fetch('assoc')) {
foreach ($collectKeys as $parts) {
$keys[$parts[0]][] = $result[$parts[1]];
if ($parts[2]) {
$keys[$parts[0]][] = $result[$parts[1][0]];
continue;
}

$collected = [];
foreach ($parts[1] as $key) {
$collected[] = $result[$key];
}
$keys[$parts[0]][] = $collected;
}
}

$statement->rewind();
}

Expand Down

0 comments on commit 193951c

Please sign in to comment.