blt04 / doctrine2-nestedset

A NestedSet extension for Doctrine2
GNU Lesser General Public License v2.1
127 stars 49 forks source link

Moving methods are broken #15

Open echo511 opened 13 years ago

echo511 commented 13 years ago

All of the methods for moving nodes can break the nested set. For instance let's have structure like this:

LFT name RGT

1 root 6 2 section 5 3 subsection 4

If I call method moveAsLastChildOf (section -> subsection) it will break the hierarchy instead of throw an exception. (issued by mlueft). For this problem I have found a simple fix:

public function moveAsLastChildOf(NodeWrapper $node)
{
    // Added ancestor check
    if($this->isAncestorOf($node) || $this == $node)
    {
        throw new \InvalidArgumentException('Cannot move node as a child of itself');
    }
    ...

Same for moveAsFirstChildOf().

The siblings methods however are more complicated especially moveAsPrevSiblingOf(subsection->section) which should be possible to do.

Any solutions?

echo511 commented 13 years ago

I just changed the first conditions of the methods to:

public function moveAsPrevSiblingOf(NodeWrapper $node)
{
    if($this->isAncestorOf($node) || $this->isRoot() || $node->isRoot() || $node == $this)
    {
        throw new \InvalidArgumentException('Cannot move node as a sibling of itself');
    }
...

So far it seems to be working.