Welcome Guest, Not a member yet? Register   Sign In
MPTT recursive search and replace problem
#1

[eluser]Gaz[/eluser]
Hi all,

I'm trying to do a recursive search and replace on an MPTT tree.

The code I'm using is as follows

Code:
function parse($nodes) {
        foreach($nodes as &$node) {
            // Replace node?
            if($node['type'] == 'ReferenceNode') {
                $n = $this->site->get_node_where('id', $node['node_ref']);
                // Replace node with node pointed to by reference
                if($n) {
                    $node = $this->site->get_node_where('object_id', $n['object_id']);
                    // Get children
                    $children = $this->site->get_descendants($node['lft'], $node['rgt']);
                    if(!empty($children)) {
                        $node['children'] = $this->parse($children);
                    }
                }
            }
        }
        return $nodes;
    }

Basically the code hangs when I run it, although it looks like it should work. If I comment out

Code:
$node['children'] = $this->parse($children);

The code works fine, but obviously doesn't deal with child nodes.

Cheers,
Gaz.
#2

[eluser]m4rw3r[/eluser]
Are you sure you won't get any "circular references" with the database keys?
(Eg. node.node_ref -> n, n.object_id -> parent_node, parent_node.descendants ∈ node Makes everything repeat itself).

Also, the get_descendants() return *all* descendants, not only children, which means that your code also iterates grandchildren which already were replaced by a recursive call to this method. Try with get_children() instead.

Another thing may be the fact that if you move nodes in the tree, you can move some nodes which already have been moved (which also can result in an endless loop).
#3

[eluser]Gaz[/eluser]
You were right. There we're circular references causing an infinite loop. D'oh!

Thanks for you help.

Cheers,
Gaz.




Theme © iAndrew 2016 - Forum software by © MyBB