Permalink
Browse files

Solving a couple bugs that were trigered when having more than one

hasMany associations to be eager loaded
  • Loading branch information...
1 parent ec35f78 commit b0dc5e66a725294352f34ae96f63f8a6fc4c6fc0 @lorenzo lorenzo committed Jan 26, 2014
Showing with 10 additions and 5 deletions.
  1. +7 −2 src/ORM/Association/HasMany.php
  2. +3 −3 src/ORM/Query.php
@@ -83,10 +83,15 @@ public function eagerLoader(array $options) {
'sort' => $this->sort(),
'strategy' => $this->strategy()
];
- $fetchQuery = $this->_buildQuery($options);
if (!empty($options['queryBuilder'])) {
- $fetchQuery = $options['queryBuilder']($fetchQuery);
+ $queryBuilder = $options['queryBuilder'];
+ unset($options['queryBuilder']);
+ }
+
+ $fetchQuery = $this->_buildQuery($options);
+ if ($queryBuilder) {
+ $fetchQuery = $queryBuilder($fetchQuery);
}
$resultMap = [];
View
@@ -1151,11 +1151,11 @@ protected function _addJoin($association, $options) {
* @return CallbackStatement $statement modified statement with extra loaders
*/
protected function _eagerLoad($statement) {
- $keys = $this->_collectKeys($statement);
+ $collected = $this->_collectKeys($statement);
foreach ($this->_loadEagerly as $meta) {
$contain = $meta['associations'];
$alias = $meta['instance']->source()->alias();
- $keys = isset($keys[$alias]) ? $keys[$alias] : null;
+ $keys = isset($collected[$alias]) ? $collected[$alias] : null;
$f = $meta['instance']->eagerLoader(
$meta['config'] + ['query' => $this, 'contain' => $contain, 'keys' => $keys]
);
@@ -1183,7 +1183,7 @@ protected function _collectKeys($statement) {
foreach ((array)$source->primaryKey() as $key) {
$pkFields[] = key($this->aliasField($key, $alias));
}
- $collectKeys[] = [$alias, $pkFields, count($pkFields) === 1];
+ $collectKeys[$alias] = [$alias, $pkFields, count($pkFields) === 1];
}
}

0 comments on commit b0dc5e6

Please sign in to comment.