-
-
Notifications
You must be signed in to change notification settings - Fork 35
Fix infinite loop when using HasSortableRelations on a model with a self-referencing relationship #178
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
Conversation
Not a fan of "somehow" issues being solved without understanding exactly how 😜 Can we dig further into why exactly an infinite loop is caused when attempting to access the relation method at this point in time? I would prefer to address that problem (or at least fully understand it) before just giving up and resorting to the relationship definition properties. Does this support relations defined via methods instead of the property approach? |
It works for all my models except one that has a relation on itself, so we need to look there.
What do you mean "relations defined via methods" ? |
|
@LukeTowers I was able to reproduce with a MUCH simpler model and a trait specifically written to only trigger this behavior: trait Test
{
public function initializeTest() : void
{
$relation = $this->myrelation();
}
}And the model: class MyModel extends \Model
{
use Test;
public $table = 'mytable';
public $belongsToMany = [
'myrelation' => [
MyModel::class,
],
];
} |
|
Not sure EXACTLY what is triggering the loop, but the trait initializes itself then for each relation instanciated (which uses the same model), the trait is probably triggered again when the relation is instanciated... This can easily be reproduced in |
|
@LukeTowers I did a bit more digging and it turns out Laravel's HasRelationships concern adds a call to |
|
So in conclusion, I think the fix in this PR is the right way to address the issue. And to address your question about relations defined in a method (I assume you meant what @bennothommo introduced in this PR, the |
|
@mjauvin Sounds good, is this something we can add a unit test for? |
|
@LukeTowers What exactly do you want to test ? |
|
Self relations not causing infinite loops :) |
|
How do you test for that? 🤣 |
- make sure models using this trait with a relation to self will not cause infinite loop.
|
I added a unittest and tested outside of this branch and it throws a fatal php error as shown below: When running under this PR, it runs as expected: |
|
@bennothommo @LukeTowers Do we really need to use phpstan? I lose so much time just trying to figureout/fix bogus issues it is not really helping... |
I have model that has a
belongsToManyrelation on itself and somehow, calling the relation on the model (i.e.$model->$relationName()) creates an infinite loop.Using only the model relation definition solves the issue.