Permalink
Browse files

Some changes to make hasMAny eager loading work after making

primaryKey() return array
  • Loading branch information...
1 parent 16b1902 commit 193951c30139a4980a6c6d33e6d326c31bdca475 @lorenzo lorenzo committed Jan 8, 2014
Showing with 25 additions and 9 deletions.
  1. +9 −6 Cake/ORM/Association/ExternalAssociationTrait.php
  2. +16 −3 Cake/ORM/Query.php
@@ -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]];
View
@@ -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();
}

0 comments on commit 193951c

Please sign in to comment.