diff --git a/src/ORM/Association.php b/src/ORM/Association.php index c78f0563a47..6ce405b81c9 100644 --- a/src/ORM/Association.php +++ b/src/ORM/Association.php @@ -771,13 +771,11 @@ protected function _bindNewAssociations($query, $surrogate, $options) $loader = $surrogate->eagerLoader(); $contain = $loader->contain(); $matching = $loader->matching(); - $target = $this->_targetTable; if (!$contain && !$matching) { return; } - $loader->attachAssociations($query, $target, $options['includeFields']); $newContain = []; foreach ($contain as $alias => $value) { $newContain[$options['aliasPath'] . '.' . $alias] = $value; diff --git a/src/ORM/EagerLoader.php b/src/ORM/EagerLoader.php index c82b1ad35df..c01eb4fa2bf 100644 --- a/src/ORM/EagerLoader.php +++ b/src/ORM/EagerLoader.php @@ -334,14 +334,22 @@ public function attachAssociations(Query $query, Table $repository, $includeFiel return; } - foreach ($this->attachableAssociations($repository) as $loadable) { - $config = $loadable->config() + [ - 'aliasPath' => $loadable->aliasPath(), - 'propertyPath' => $loadable->propertyPath(), - 'includeFields' => $includeFields, - ]; - $loadable->instance()->attachTo($query, $config); - } + $attachable = $this->attachableAssociations($repository); + $processed = []; + do { + foreach ($attachable as $alias => $loadable) { + $config = $loadable->config() + [ + 'aliasPath' => $loadable->aliasPath(), + 'propertyPath' => $loadable->propertyPath(), + 'includeFields' => $includeFields, + ]; + $loadable->instance()->attachTo($query, $config); + $processed[$alias] = true; + } + + $newAttachable = $this->attachableAssociations($repository); + $attachable = array_diff_key($newAttachable, $processed); + } while ($attachable); } /**