Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Handle nested relations #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Concerns/AutoloadsRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait AutoloadsRelationships
/**
* @var ?Collection
*/
protected $parentCollection = null;
public $parentCollection = null;

/**
Comment on lines +27 to 29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public $parentCollection = null;
/**
public $parentCollection = null;
public $caller;
/**

* @var ?LoggerInterface
Expand Down Expand Up @@ -112,7 +112,9 @@ private function logAutoload(string $relationship)
*/
public function getRelationshipFromMethod($method)
{
$fqRelation = "";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$fqRelation = "";

$relation = $this->$method();
$fqRelation = $method;

if (!$relation instanceof Relation) {
throw new LogicException(
Expand All @@ -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];
Comment on lines +136 to 138

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$collection = $this->relations[$method];
$collection->caller = (object) ['method' => $method, 'model' => $this];
return $this->relations[$method];
$relation = $this->relations[$method];
$relation->caller = (object) ['method' => $method, 'model' => $this];
return $relation;

}
}
Expand Down