views:

134

answers:

1

For all of you who work with Doctrine, I've a question related to moveAsLastChildOf(). I'm using Doctrine 1.1.6 and Symfony 1.2

I have a nested tree, with lots of elements. I want to delete an element of the tree, so first I have to move all its children (and descendants) to its parent; and then delete it. I do the following:

// Get the parent
$parent = $object->getParent();

if ($object->getNode()->isValidNode())
{
  $children = $object->getNode()->getChildren();
  // Move each child to the parent of this object (grandparent of the children)
  foreach($children as $child){
    $child->getNode()->moveAsLastChildOf($parent);
  }
  // and then delete the element
  $object->getNode()->delete();
}
else
{
  $object->delete();
}

But it's not working, even children are deleted. And the element I wanted to remove is still on the DB with non-sense lft&rgt. The log of that example:

parent id=2412 lft(2044) rgt(2523) level(5)
to lft(2044) rgt(2523) level(5)

object to delete id=2487 lft(2375) rgt(2384) level(6)
to lft(2375) rgt(2509) level(6) *non deleted and with non-sense lft & rgt*

id=2491 lft(2376) rgt(2377) level(7)
to lft(2521) rgt(2522) level(6)

id=2490 lft(2378) rgt(2379) level(7)
to lft(2376) rgt(2377) level(6) *deleted*

id=2489 lft(2380) rgt(2381) level(7)
to lft(2519) rgt(2520) level(6)

id=2488 lft(2382) rgt(2383) level(7)
to lft(2378) rgt(2379) level(6) *deleted*

I've done a quick look at the Doctrine code and I haven't found anything wrong. I've to check it better. I'm going to ask also to the doctrine team to be sure it's my problem or not.

+1  A: 

You can use this as workaround (the key is to refresh every node whenever you change the tree, so I just get 1 child in turn and at the end refresh the original node):

    $node = $tag->getNode();
    if ($node->hasParent()) {
        $parent = $node->getParent();

        while ($child = $node->getFirstChild()) {
            $child->getNode()->moveAsLastChildOf($parent);
        }
    } else {
        while ($child = $node->getFirstChild()) {
            $child->getNode()->makeRoot($child->id);
        }
    }
    $tag->refresh()->getNode()->delete();

I am using Doctrine 1.2, but I think there is no difference.

Vladimir
thanks a lot :-)
fesja