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

Allows a Children to be extended #115

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

RomainMazB
Copy link

Hi!

Working in a game environment, I have a Building model instance using STI to store buildings like "tree", "oasis", "house" and so on.

class Building extends Model
{
    use \Parental\HasChildren;
}

class Tree extends Building
{
    use \Parental\HasParent;
}

class Oasis extends Building
{
    use \Parental\HasParent;
}

I wanted to extend the Oasis class into a GreatOasis one with some difference into the class constants to slightly improve the building's performance.

class GreatOasis extends Oasis
{
    public const MINIMUM_VITALITY = 40;
}

The issue is that when the package looks for the parent class from the GreatOasis class, it fails because its direct parent is Oasis and not the Building model class, so the table name results in oases instead of buildings:

Next Illuminate\Database\QueryException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "oases" does not exist
LINE 1: update "oases" set "vitality" = $1, "updated_at" = $2 where ...
^ (Connection: pgsql, SQL: update "oases" set "vitality" = 70, "updated_at" = 2023-08-24 15:08:43 where "id" = 2732) in /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:793

With this PR, it allows a Children to be extended as many as wanted, the parent will be defined by the first parent class using the HasParent trait, or defaults to its direct parent.

@@ -146,6 +146,21 @@ protected function getParentClass(): string
{
static $parentClassName;

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

Choose a reason for hiding this comment

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

$parentClassName was set to static for performance reasons.
This update returns $parentClassName if it is set, but never sets it which could have performance implications.

See my related comment from PR #104.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants