Skip to content

Commit d785d98

Browse files
markstorylorenzo
authored andcommitted
Fix belongsToMany with deeper associations not loading.
When deep associations are loaded from junction tables, they are loaded as a hasMany. This means we need to accomodate for this structure as well. This solution doesn't make me happy but it was the smallest change I could make. Other approaches I took caused other things to break.
1 parent f67d786 commit d785d98

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/ORM/Association/BelongsToMany.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,14 @@ protected function _buildResultMap($fetchQuery, $options)
468468
$property
469469
));
470470
}
471-
$result[$this->_junctionProperty] = $result[$property];
471+
$junctionData = $result[$property];
472+
473+
// Account for incorrectly nested data from hasMany
474+
// bindings when junction tables have additional associations loaded
475+
if (isset($result[$property][0])) {
476+
$junctionData = $result[$property][0];
477+
}
478+
$result[$this->_junctionProperty] = $junctionData;
472479
unset($result[$property]);
473480

474481
if ($hydrated) {
@@ -1282,7 +1289,12 @@ protected function _buildQuery($options)
12821289
->where($this->junctionConditions())
12831290
->select($query->aliasFields((array)$assoc->foreignKey(), $name));
12841291

1285-
$assoc->attachTo($query);
1292+
// If this hasMany attaches any other containments, then the junction
1293+
// table is queried twice. Ideally the EagerLoader could resolve the duplication,
1294+
// and merge the resulting eagerloadable instances.
1295+
//
1296+
// We have to define aliasPath, so any nested contains will work.
1297+
$assoc->attachTo($query, ['aliasPath' => $assoc->alias()]);
12861298

12871299
return $query;
12881300
}

0 commit comments

Comments
 (0)