You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I implemented NodeTrait trait on an abstract class, then modified its ancestors() method to use newModelQuery() instead of newQuery() 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 the parent_id (see below). Also, on one of the child class, a creating() model event is applied to ensure the model is always saved as root.
abstractclass Category extends Model
{
use NodeTrait;
publicfunctionancestors()
{
returnnewAncestorRelation($this->newModelQuery(), $this);
}
}
class RootCategory extends Category
{
protectedstaticfunctionbooted(): void
{
static::addGlobalScope('rootScope', function (Builder$builder): void {
$builder->whereNull('parent_id');
});
static::creating(function (self$rootCategory): void {
$rootCategory->makeRoot();
});
}
}
class SubCategory extends Category
{
protectedstaticfunctionbooted(): void
{
static::addGlobalScope('subScope', function (Builder$builder): void {
$builder->whereNotNull('parent_id');
});
}
}
How to reproduce:
$rootCategory = RootCategory::create(['name' => fake()->word()]);
// returns the created model with:// id: 1// parent_id: null// _lft: 1// _rgt: 2$subCategory = SubCategory::create(['name' => fake()->word()], $rootCategory);
// returns the created model with:// id: 2// parent_id: 1// _lft: 2// _rgt: 3$rootCategory->refresh();
// the _lft and _rgt is still 1 and 2, not 1 and 4 as I was expected
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:
I found the solution for the above issue.
So, what happened is because there is global scopes applied, the newQuery() of the model will always returned a query builder with scopes applied.
Upon inserting a node into a parent, the package uses the newQuery() method (or withTrashed()), see NodeTrait.php#L674.
The solution is either override the newNestedSetQuery() method or directly override the model's newQuery() method. For this case, I override the newNestedSetQuery() method because this happens only on this package. Modifying the newQuery() method would cause any package or code that calls it changed, which is currently not what I want to achieve.
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.
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: