From 00f90a4e17e45d117b7ec1acd4456e90428a6636 Mon Sep 17 00:00:00 2001 From: Liam Wiltshire Date: Mon, 21 Oct 2019 20:20:57 +0100 Subject: [PATCH] [WIP] Handle nested relations --- src/Concerns/AutoloadsRelationships.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Concerns/AutoloadsRelationships.php b/src/Concerns/AutoloadsRelationships.php index 125d4ff..afb3091 100644 --- a/src/Concerns/AutoloadsRelationships.php +++ b/src/Concerns/AutoloadsRelationships.php @@ -24,7 +24,7 @@ trait AutoloadsRelationships /** * @var ?Collection */ - protected $parentCollection = null; + public $parentCollection = null; /** * @var ?LoggerInterface @@ -112,7 +112,9 @@ private function logAutoload(string $relationship) */ public function getRelationshipFromMethod($method) { + $fqRelation = ""; $relation = $this->$method(); + $fqRelation = $method; if (!$relation instanceof Relation) { throw new LogicException( @@ -122,9 +124,17 @@ public function getRelationshipFromMethod($method) if ($this->shouldAutoLoad()) { if (!$this->relationLoaded($method)) { - $this->logAutoload($method); - $this->parentCollection->load($method); + $parentCollection = $this->parentCollection; + while (isset($parentCollection->caller)) { + $fqRelation = $parentCollection->caller->method . "." . $fqRelation; + $parentCollection = $parentCollection->caller->model->parentCollection; + } + $this->logAutoload($fqRelation); + $parentCollection->loadMissing($fqRelation); + + $collection = $this->relations[$method]; + $collection->caller = (object) ['method' => $method, 'model' => $this]; return $this->relations[$method]; } }