Skip to content

Deleting node throws "Model must be node" exception when descendants are deleted #363

Open
@jengel3

Description

@jengel3

Not sure what's going on here --

I've got a MenuItem class that represents the items in a tree-like navigation menu:

class MenuItem extends BaseModel
{
    use NodeTrait;

    protected $primaryKey = 'menu_item_id';
    protected $fillable = ['menu_item_id', 'left', 'right', 'parent_item_id', 'grouping_id'];
    protected $table = 'store_menu_items';

    public function getLftName ()
    {
        return 'left';
    }

    public function getRgtName ()
    {
        return 'right';
    }

    public function getParentIdName ()
    {
        return 'parent_item_id';
    }
}

Now, when I go to delete a node say $node = MenuItem::query()->find($someId); $node->delete() .. the node itself is deleted, but throws an exception when it tries to delete descendants.

However, if I call $node->deleteDescendants() then all the descendants are deleted without error, and of course the node itself is still present. If I call $node->delete() after doing that, the previous error is still thrown. I'm not sure why calling deleteDescendants() directly wouldn't throw that same error, considering that the deleted event linked above is going to call the exact same method eventually.

I've tracked it down to NestedSet::isNode(..) here which seems to return false when checking if the NodeTrait class is being used by the model.. which it clearly is in the above code. If I change that method to use PHP's class_uses method then the trait is definitely included in the outputted array, but for whatever reason isn't when it's being casted to an array in isNode(..).

Any idea what would be causing this? Could it be that I'm not using the typical primary keys or nestedset column names? I feel like a PR to use the class_uses method would solve this, but it seems to work for everyone else so I'm not sure why this is an issue in my code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions