-
Notifications
You must be signed in to change notification settings - Fork 472
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
Parent _lft and _rgt not updated after adding child using create()
method
#591
Comments
I found the solution for the above issue. The solution is either override the Lastly, I wonder if this is the solution or there was another built-in solution on this package in order to apply global scopes without affecting the query builder. abstract class Category extends Model
{
use NodeTrait;
public function ancestors()
{
return new AncestorRelation($this->newModelQuery(), $this);
}
public function newNestedSetQuery($table = null)
{
$builder = $this->usesSoftDelete()
? $this->withTrashed()
: $this->newQuery();
return $this->applyNestedSetScope(($builder->withoutGlobalScopes(), $table);
}
} |
In addition to solution above, below is the current working solution. abstract class Category extends Model
{
use NodeTraitWithGlobalScope, SoftDeletes;
}
trait NodeTraitWithGlobalScope
{
use NodeTrait;
public function descendants()
{
return new DescendantsRelation($this->newNestedSetQuery(), $this);
}
public function ancestors()
{
return new AncestorsRelation($this->newNestedSetQuery(), $this);
}
public function newNestedSetQuery($table = null)
{
$builder = $this->usesSoftDelete()
? $this->withTrashed()
: $this->newQuery();
return $this->applyNestedSetScope($builder->withoutGlobalScopes(), $table);
}
public function newScopedQuery($table = null)
{
return $this->applyNestedSetScope($this->newNestedSetQuery(), $table);
}
} |
I implemented
NodeTrait
trait on an abstract class, then modified itsancestors()
method to usenewModelQuery()
instead ofnewQuery()
in order to retrieve ancestors without using any global scopes that is implemented on the child class. On the child class, a global scope is applied to query based on theparent_id
(see below). Also, on one of the child class, acreating()
model event is applied to ensure the model is always saved as root.How to reproduce:
The
_lft
and_rgt
of the$rootCategory
on the database is also 1 and 2.Was this expected?
I'll provide more codes if needed.
Thanks!
The text was updated successfully, but these errors were encountered: