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

Allow a model to be both a parent and child #104

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/HasChildren.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat
$instance = $this->newRelatedInstance($related);

if (is_null($foreignKey) && $instance->hasParent) {
$foreignKey = Str::snake($instance->getClassNameForRelationships()).'_'.$instance->getKeyName();
$foreignKey = Str::snake($instance->getClassNameForHasChildrenRelationships()).'_'.$instance->getKeyName();
}

if (is_null($relation)) {
Expand Down Expand Up @@ -184,7 +184,7 @@ public function belongsToMany(
$instance = $this->newRelatedInstance($related);

if (is_null($table) && $instance->hasParent) {
$table = $this->joiningTable($instance->getClassNameForRelationships());
$table = $this->joiningTable($instance->getClassNameForHasChildrenRelationships());
}

return parent::belongsToMany(
Expand All @@ -201,7 +201,7 @@ public function belongsToMany(
/**
* @return string
*/
public function getClassNameForRelationships(): string
public function getClassNameForHasChildrenRelationships(): string
{
return class_basename($this);
}
Expand Down
15 changes: 10 additions & 5 deletions src/HasParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ public function getForeignKey(): string
*/
public function joiningTable($related, $instance = null): string
{
$relatedClassName = method_exists((new $related), 'getClassNameForRelationships')
? (new $related)->getClassNameForRelationships()
$relatedClassName = method_exists((new $related), 'getClassNameForHasParentRelationships')
? (new $related)->getClassNameForHasParentRelationships()
: class_basename($related);

$models = [
Str::snake($relatedClassName),
Str::snake($this->getClassNameForRelationships()),
Str::snake($this->getClassNameForHasParentRelationships()),
];

sort($models);
Expand All @@ -99,7 +99,7 @@ public function joiningTable($related, $instance = null): string
* @return string
* @throws ReflectionException
*/
public function getClassNameForRelationships(): string
public function getClassNameForHasParentRelationships(): string
{
return class_basename($this->getParentClass());
}
Expand Down Expand Up @@ -127,6 +127,11 @@ protected function getParentClass(): string
{
static $parentClassName;

return $parentClassName ?: $parentClassName = (new ReflectionClass($this))->getParentClass()->getName();
if ($parentClassName) return $parentClassName;

$parentClassName = (new ReflectionClass($this))->getParentClass()->getName();

$parent = new $parentClassName;
return $parent->hasParent === true ? $parent->getParentClass() : $parentClassName;
Comment on lines 128 to +135
Copy link
Member

Choose a reason for hiding this comment

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

This may not work as expected.

Initially $parentClassName was set as static to cache the value. If $parentClassName had a value, it was returned; otherwise, a value was generated, assigned, then returned. The next time getParentClass() was called, the cached value was returned.

The updated code returns $parentClassName if a value exists.
If a value does not exist, one is generated and assigned.
Then we new up the parent class, and check if the parent class has a parent, if it does we call getParentClass() and return the result. The next time this method is called we will return the cached $parentClassName and not the result of $parent->getParentClass().

Does that make sense?

}
}