laravel-nestedset icon indicating copy to clipboard operation
laravel-nestedset copied to clipboard

isLeaf() when using SoftDeletes

Open isecchin opened this issue 7 years ago • 7 comments

The isLeaf() method works as expected, but when using on a model with SoftDeletes, whenever you delete the single child of a node, the parent node should be considered as a leaf, IMO, but that's not the case, since the node is still present on the database and the implementation of the isLeaf() method currently just checks if there is nothing between the left and right attributes of the node.

A simple solution would be to change how the isLeaf() method verifies if the node is indeed a leaf:

/**
 * Checks whether the node is a leaf
 *
 * @return bool
 */
public function isLeaf()
{
    return $this->children->isEmpty();
}

isecchin avatar Mar 31 '18 02:03 isecchin

Hm... This is not a great solution either. I wouldn't want to run hidden queries. Maybe I should handle soft deleting differently, like maybe making nodes rooted.

lazychaser avatar Mar 31 '18 05:03 lazychaser

@lazychaser , that would be a perfect solution, at least for me!

In a menu structure, if I soft delete a menu item with childs, parent and childs would become rooted nodes.

Will you implement this feature?

lparede avatar Apr 08 '18 13:04 lparede

Uuhn... I don't know... Indeed it is a possible solution, maybe better than running a query for this, but implementing it this way will also compromise the restoring feature. I mean, if you make the children nodes rooted you fix the problem of the isLeaf() method that I mentioned before, but then if at some point in time you need to restore any of the children nodes, you would have lost their position, and I think that when you restore something, you'd expect it to be the way it was when you deleted, and that includes the original position. This is a minor issue, and doesn't affect me much (at least for my use case), it is just a small inconvenience that we may have to reposition the node again when we restore it, but maybe it's worth considering.

isecchin avatar Apr 09 '18 02:04 isecchin

@isecchin , yes on restore the original position will be lost. Also, in my case, this won't give me any trouble at all.

lparede avatar Apr 09 '18 20:04 lparede

I just recently encountered this issue. Will this issue be revisited at some point?

d3radicated avatar Jan 21 '20 05:01 d3radicated

Any update?

cerw avatar Aug 21 '20 07:08 cerw

I always cry inside - why my use case requires the thing that was identified and never implemented :cry:

So using SoftDeletes would require either additional queries for isLeaf() function?

Another approach I though of is taking SoftDeleted item out of tree, by also updating _lft _rgt values accordingly. Tree broken/fixing functions however might not like this.

liepumartins avatar Feb 07 '22 12:02 liepumartins