Skip to content

Commit 2ea0c63

Browse files
committed
fix: use full relation path as key in eager relations
1 parent d7b0f8c commit 2ea0c63

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

packages/database/src/BelongsToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function getSelectFields(): ImmutableArray
5454
->map(map: fn (
5555
$field,
5656
) => new FieldStatement(
57-
field: $this->getTableAlias(tableName: $targetModel->getTableName()) . '.' . $field,
57+
field: "{$this->getTableAlias(tableName: $targetModel->getTableName())}.{$field}",
5858
)
5959
->withAlias(
6060
alias: sprintf(

packages/database/src/Builder/ModelInspector.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,12 @@ public function resolveRelations(string $relationString, string $parent = '', ar
606606
$relationModel = inspect($currentRelation);
607607
$modelType = $relationModel->getName();
608608

609+
$fullPath = $parent !== ''
610+
? "{$parent}.{$currentRelationName}"
611+
: $currentRelationName;
612+
609613
if (in_array($modelType, $visitedPaths, true)) {
610-
return [$currentRelationName => $currentRelation->setParent($parent)];
614+
return [$fullPath => $currentRelation->setParent($parent)];
611615
}
612616

613617
$newRelationString = implode('.', $relationNames);
@@ -618,7 +622,7 @@ public function resolveRelations(string $relationString, string $parent = '', ar
618622
$currentRelationName,
619623
), '.');
620624

621-
$relations = [$currentRelationName => $currentRelation];
625+
$relations = [$fullPath => $currentRelation];
622626

623627
return [
624628
...$relations,
@@ -659,7 +663,10 @@ public function resolveEagerRelations(string $parent = '', array $visitedPaths =
659663
continue;
660664
}
661665

662-
$relations[$property->getName()] = $currentRelation->setParent($parent);
666+
$fullPath = $parent !== ''
667+
? "{$parent}.{$currentRelationName}"
668+
: $currentRelationName;
669+
$relations[$fullPath] = $currentRelation->setParent($parent);
663670
$newVisitedPaths = [...$visitedPaths, $this->getName()];
664671

665672
foreach ($relationModel->resolveEagerRelations($newParent, $newVisitedPaths) as $name => $nestedEagerRelation) {

0 commit comments

Comments
 (0)