EllisLab text mark
Advanced Search
5 of 10
5
   
MPTtree, Hieararchical trees in a database table
Posted: 15 October 2008 02:37 AM   [ # 61 ]   [ Rating: 0 ]
Avatar
Joined: 2007-10-31
42 posts

Oopy.. I found the reason. It’s because in node_text it’s using the ‘slug’ index but i have ‘title’ smile

 
Posted: 15 October 2008 02:45 AM   [ # 62 ]   [ Rating: 0 ]
Avatar
Joined: 2007-10-31
42 posts

..but now i got another problem. When i try to add a new child node it does something with mysql and after that i only see is:

Error Number1100
Table 
'sessions' was not locked with LOCK TABLES 

and only after restarting mysql server i can see admin_tree main page again..

 
Posted: 15 October 2008 04:17 AM   [ # 63 ]   [ Rating: 0 ]
Avatar
Joined: 2008-05-17
1073 posts

You probably misconfigured it somehow… please post some code!
How are you using the “sessions” table combined with MPTtree?
Are you using the current Version of MPTtree (0.1.6-fix2) ?

Btw, I would recommend also using the latest Ext JS (which is v 2.2 at the moment).

If you want to, I can post an updated example…

 
Posted: 15 October 2008 12:11 PM   [ # 64 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-14
95 posts

similar to the suggested update_node_by_id function I’ve just added a method on my own version to reinsert a node:

function reinsert_node($lft,$arg,$lock true){
        $root 
$this->get_root();
        if(
$lft $root[$this->right_col] || $lft 1)return false;
        if (
$lock)
            
$this->lock_tree_table();

        
$this->db->query('UPDATE '.$this->tree_table.
                        
' SET '.$this->left_col.' = '.$this->left_col.' + 2 '.
                        
' WHERE '.$this->left_col.' >= '.$lft);
        
$this->db->query('UPDATE '.$this->tree_table.
                        
' SET '.$this->right_col.' = '.$this->right_col.' + 2 '.
                        
' WHERE '.$this->right_col.' >= '.$lft);
    
        
$this->db->where($arg);
        
$this->db->update($this->tree_table,array($this->left_col => $lft,$this->right_col => $lft+1));
        if (
$lock)
            
$this->unlock_tree_table();
        return 
true;
    

The idea is to take a record already in the table and add it back into the tree.

 Signature 

Trying to do it right the first time! whoops guess that didn’t work out so well…

 
Posted: 15 October 2008 12:45 PM   [ # 65 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-03
671 posts

Maybe there is a wrong uri?
It was really some time since I’ve looked at that source (and I was rewriting MPTTree when I suddenly got other things to do),
so I’m sorry if I’m not so good at helping you for the moment.

 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: 17 October 2008 10:12 AM   [ # 66 ]   [ Rating: 0 ]
Avatar
Joined: 2007-10-31
42 posts

In tree_admin, after right clicking on the root node (which i’ve added manually) and then clicking New page->New child page what should i see? I don’t see anything and there is an error in firebug:

missing } in XML expression
[Break on this error] background
-color#fff;\n
ext-all.js (line 29

But if i click on ext-all-js i see normal ext-all.js source..

 
Posted: 28 October 2008 03:45 PM   [ # 67 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-03
671 posts

For all those who are asking for the 1.6-fix2 version (the latest, which was posted on CI base), here it is.

 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: 12 November 2008 10:46 AM   [ # 68 ]   [ Rating: 0 ]
Avatar
Joined: 2008-09-14
24 posts

this is a really useful addition and I plan to post my controller implementation of it when I’ve finished.

i have a problem when trying to move a node i.e. the node does not move using the following. I have the lft ids of each node in a tree passed in as $nodeid

function moveNodeUp($nodeid){
    $node 
intval($nodeid);
    if (
$node == 1){
        
echo 'node is root';
exit();
    
}
        
    $tnode 
$this->backend->getNodeLeft($node 1); // returns the lft value using rgt
    
if ($tnode){
        $src 
$this->getNodeById($node);
        
$tgt $this->getNodeById($tnode);
        
$res =  $src->move_above($tgt);
        echo 
'moved:' $res;
    
else {
        
echo 'target doesn\'t exist';
    }
        
    $this->index();
        } 

thanks

anton

 Signature 

” ...the announcement of a vast project is always its betrayal” - Bataille

 
Posted: 14 November 2008 01:30 PM   [ # 69 ]   [ Rating: 0 ]
Joined: 2007-10-20
12 posts

I’m having problems with this..

$this->load->MPTT('user_tree');
$this->user_tree->insert_root(array('title'=>'FAS''userid'=>'1')); 

PHP Fatal error:  Cannot pass parameter 1 by reference in /home/camalotlms.com/system/application/models/mpttree.php on line 405

Heres the function that the error is in..

function get_root(){
        $query 
$this->db->getwhere($this->tree_table,array($this->left_col => 1),1);
        
$return $query->num_rows() ? $query->row_array() : false;
        if(!
$return)
            
$this->debug_message('Root node was not found.'); // this is like 405 with the error..
        
return $return;
    
 
Posted: 14 November 2008 01:50 PM   [ # 70 ]   [ Rating: 0 ]
Joined: 2007-10-20
12 posts

Found the issue. It had to to with the debug_message function

function debug_message(&$message){
  
...

Passing the $message var by referance using & was causing the issue. I deleted the & and it works fine.

Would like to know why this caused an issue on my end.. kinda odd..

 
Posted: 14 November 2008 05:33 PM   [ # 71 ]   [ Rating: 0 ]
Joined: 2007-10-20
12 posts

Thank you m4rw3r for writing this, you saved me countless hours! I did a quick test with 10,000 items and about 50 deep. The inserting took about 4 minutes, but when reading the data.. its deadly quick, very amazing!

Thanks again!

 
Posted: 15 November 2008 08:41 AM   [ # 72 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-03
671 posts

That is the point with Nested Sets, fast retrieval but slower inserts/moves.

I have “abandoned” MPTtree for a while, because other projects eats a lot of my time (mainly my schoolwork and IgnitedRecord), but I will try to get some time after IgnitedRecord 1.0 is finished.
That is the reason why I haven’t made any updates/bugfixes to MPTtree, because I have plans to make an almost complete rewrite.

 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: 05 January 2009 04:02 PM   [ # 73 ]   [ Rating: 0 ]
Joined: 2007-05-21
340 posts
function tree2array($root 1){

        $node 
$this->get_node($root);
        if(
$node == false)
            return 
false;
        
// query
        
$query $this->db->query('SELECT * FROM '.$this->tree_table.
            
' WHERE '.$this->left_col.' BETWEEN '.$node[$this->left_col].
            
' AND '.$node[$this->right_col].
            
' ORDER BY lft ASC');
            
        
$right = array();
        
$result = array();
        
$current =& $result;
        
$stack = array();
        
$stack[0] =& $result;
        
$lastlevel 0;
        foreach(
$query->result_array() as $row){
            
// go more shallow, if needed
            
if(count($right)){
                
while($right[count($right)-1] $row[$this->right_col]){
                    array_pop
($right);
                
}
            }
            
// Go one level deeper?
            
if(count($right) > $lastlevel){
                end
($current);
                
$current[key($current)]['children'= array();
                
$stack[count($right)=& $current[key($current)]['children'];
            
}
            
// the stack contains all parents, current and maybe next level
            
$current =& $stack[count($right)];
            
// add the data
            
$current[] $row;
            
// go one level deeper with the index
            
$lastlevel count($right);
            
$right[] $row[$this->right_col];
        
}
        
return $result;
    

 

You prolly remember that one, i try to think about the best way to sort those by title by recursive sub indeed.

You work on that kind of sorting with tree2array already ?

thanks a lot.

 Signature 

-> None official irc channel [ irc.freenode.net #codeigniter ]

 
Posted: 06 January 2009 12:15 PM   [ # 74 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-03
671 posts

No, because the thing with nested sets is that I cannot really order them by anything else than lft in SQL while maintaining the tree structure.

This is both good and bad, in your case it poses a problem, because you have to fetch them like you traverse an adjacency list, but the good thing is that it supports custom ordering by default.

I’m going to make a rewrite of MPTtree someday, but I’ll be when I have time for it (currently I have work + school + IgnitedRecord, and it is a lot).

 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: 06 January 2009 03:44 PM   [ # 75 ]   [ Rating: 0 ]
Joined: 2007-05-21
340 posts

By the way, did i miss something or i don’t see a way to easyly move parent branche and below child above another parent branche ?

Or i missed something.

Just to confirm.

wish you the best m4rw3r and thanks a lot for sharing!

see ya around.

 Signature 

-> None official irc channel [ irc.freenode.net #codeigniter ]

 
5 of 10
5