Browse Source

Solving a couple bugs that were trigered when having more than one
hasMany associations to be eager loaded

Jose Lorenzo Rodriguez 12 years ago
parent
commit
b0dc5e66a7
2 changed files with 10 additions and 5 deletions
  1. 7 2
      src/ORM/Association/HasMany.php
  2. 3 3
      src/ORM/Query.php

+ 7 - 2
src/ORM/Association/HasMany.php

@@ -83,10 +83,15 @@ class HasMany extends Association {
 			'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 = [];

+ 3 - 3
src/ORM/Query.php

@@ -1151,11 +1151,11 @@ class Query extends DatabaseQuery {
  * @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 @@ class Query extends DatabaseQuery {
 				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];
 			}
 		}