EllisLab text mark
Advanced Search
     
MPTT recursive search and replace problem
Posted: 02 July 2009 10:18 AM   [ Ignore ]
Joined: 2008-08-15
36 posts

Hi all,

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

The code I’m using is as follows

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

$node['children'$this->parse($children); 

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

Cheers,
Gaz.

 
Posted: 04 July 2009 06:50 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-03
671 posts

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).

 Signature 

RapidDataMapper: My new ORM, is now released!

IgnitedRecord: Old ORM

MPTtree: A model to handle trees in a database.

YAYParser - Yet Another YAML Parser

 
Posted: 14 July 2009 12:16 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2008-08-15
36 posts

You were right. There we’re circular references causing an infinite loop. D’oh!

Thanks for you help.

Cheers,
Gaz.