laravel-nestedset
laravel-nestedset copied to clipboard
isLeaf() when using SoftDeletes
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();
}
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 , 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?
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 , yes on restore the original position will be lost. Also, in my case, this won't give me any trouble at all.
I just recently encountered this issue. Will this issue be revisited at some point?
Any update?
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.