MPTtree, Hieararchical trees in a database table
Posted: 17 May 2008 07:51 PM
[ Ignore ]
[ # 21 ]
[ Rating: 0 ]
Joined: 2006-10-17 207 posts
First try to find the leaves:
select from tree where lft = rgt - 1
Then for each do
select from tree where visible = 1 and lft <= $lft and rgt >= $rgt
Then check if the lineage is correct, I don’t know a way to handle all you want in one query. This will select all visible parents. I usually store the parent_id in a column with mptt so I can derive whether a parent is missing. The other way would be to get the entire lineage of a leaf and then see with a loop in php whether all parents are visible.
I’m not sure it can be done any faster, maybe the more sql proficient know.
Posted: 20 May 2008 01:06 PM
[ Ignore ]
[ # 22 ]
[ Rating: 0 ]
Joined: 2007-04-16 113 posts
This looks great. Thanks for the contribution!
I wonder, is there some way of making the tree2array work nicely with CodeIgniters ol() or ul() HTML-helper?
Signature
EVERY TIME YOU ASK ABOUT CODEIGNITER 2.0… PHIL STURGEON KILLS A KITTEN
Posted: 30 May 2008 02:24 PM
[ Ignore ]
[ # 23 ]
[ Rating: 0 ]
Joined: 2006-08-03 671 posts
Ok, been busy and now I’ve noticed your question when checking all suggestions (be patient, I’ll implement some of them), so here comes the answer:
No, I’m afraid not.
You have to either convert this:
array( [0] => array( 'id' => '1' , 'title' => 'A name' , 'children' => array( [0] => array( 'id' => '2' , 'title' => 'Another name' ) ) ) )
To
array( 'A name' => 'Another name' );
Or you could use something like this:
function MPTtree_ul ( $array ) { $str = '<ul>' ; foreach( $array as $data ) $str .= '<li>' ; $str .= '<a href="' . $data[ 'id' ] . '">' . $data[ 'title' ] . '</a>' ; // whatever you want between the <li> </li> if(isset( $data[ 'children' ] )) { $str .= MPTtree_ul ( $data[ 'children' ] ); } $str .= '</li>' ; } $str .= '</ul>' ; return $str ; }
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: 30 May 2008 06:12 PM
[ Ignore ]
[ # 24 ]
[ Rating: 0 ]
Joined: 2007-06-07 690 posts
Hey, I just understood the whole “MPTree” concept. I actually started implementing the same thing for the CodeExtinguisher site ( http://71.65.20.84:81/codexsite/index.php/docs/setup )
I remember you were interested in implementing this feature into CodeExtinguisher. If you still are, I’m willing to work through it with you. I haven’t actually tried MPTree yet, but it seems perfect for my use.
Signature
jtaby.com
Posted: 30 May 2008 06:57 PM
[ Ignore ]
[ # 25 ]
[ Rating: 0 ]
Joined: 2006-08-03 671 posts
I think it would be a very nice addition, both to CodeExtinguisher and MPTtree, to have a good admin interface for storing tree data in a database.
I’m going to make some other additions to MPTtree and it would be nice to make a release “with” a full admin interface, so I’ll gladly work with you.
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: 30 May 2008 06:58 PM
[ Ignore ]
[ # 26 ]
[ Rating: 0 ]
Joined: 2007-06-07 690 posts
Cool, I can include this as a “view mode”. Take a look at the preview (in my signature), and see how there is the ability to use different view_modes? If you want more info on view_modes in the context of CodeExtinguisher, take a look at the temporary docs also linked to in my signature.
Signature
jtaby.com
Posted: 30 May 2008 07:08 PM
[ Ignore ]
[ # 27 ]
[ Rating: 0 ]
Joined: 2006-08-03 671 posts
Ummm, the view_modes page in your wiki is empty.
But I think it’s not a bad idea. It could be useful with also a “flat” table manipulation option.
The things needed are a few buttons/drag-drop, etc.
Can view modes handle this in a nice way?
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: 30 May 2008 07:17 PM
[ Ignore ]
[ # 28 ]
[ Rating: 0 ]
Joined: 2007-06-07 690 posts
oh yeah, i thought I had written that page. In any case, yeah, each file in CodeExtinguisher can load its own assets.
Signature
jtaby.com
Posted: 30 May 2008 07:22 PM
[ Ignore ]
[ # 29 ]
[ Rating: 0 ]
Joined: 2006-08-03 671 posts
That sounds fine.
Maybe we should continue this discussion via MSN or skype.
(While this is increasing our post count , I don’t feel like filling a whole thread with a conversation)
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: 30 May 2008 07:24 PM
[ Ignore ]
[ # 30 ]
[ Rating: 0 ]
Joined: 2007-06-07 690 posts
haha yes I don’t want to steal your thread, could you PM me your email? gmail, msn or skype work for me
Signature
jtaby.com
Posted: 11 June 2008 03:42 PM
[ Ignore ]
[ # 31 ]
[ Rating: 0 ]
Joined: 2006-08-03 671 posts
I’ve now started to code on MPTtree version 0.2
Some things I’ll add/edit:
- The Fusion feature, the addition of parent_id (an optional option, a fusion between Nested Sets and Adjacency list)
- Remake the locking system (not so much, but it will hopefully make it easier for me)
- A few new methods for the ORM (will be added in ignitedRecord as well)
- Renaming of a few ORM methods, to not confuse the user when using MPTtree and IgnitedRecord
So I ask:
Are there any specific features/changes you want to see in MPTtree 0.2?
PS. Wondering about if I should drop the ORM feature of MPTtree and concentrate my ORM work on IgnitedRecord, what do you think?
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: 01 August 2008 11:44 AM
[ Ignore ]
[ # 32 ]
[ Rating: 0 ]
Joined: 2008-08-01 3 posts
Hi there,
thank you for sharing this nice work ...
Is it possible to have multiple trees in one db-table?
Greets,
yami
Posted: 01 August 2008 04:05 PM
[ Ignore ]
[ # 33 ]
[ Rating: 0 ]
Joined: 2006-08-03 671 posts
No, but you can use a common root node for all the trees in that table:
root ------ sometree ----- somenode1 | \ -- somenode2 ... ... |--- anothertree -- anothernode1 | \ - anothernode2 ... ...
BTW. A lot of my time is currently spent on coding IgnitedRecord, but I’ll soon resume with the development of MPTtree
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: 01 August 2008 05:48 PM
[ Ignore ]
[ # 34 ]
[ Rating: 0 ]
Joined: 2008-01-07 66 posts
MPTree @version 0.1.6-fix
The mpttree->move_node_append($move_lft,$to_lft); cast an error..
However, the function does what it’s supposed to!
A PHP Error was encountered Severity : Notice Message : Undefined index : lft Filename : models / mpttree . php Line Number : 1323
Great model!
I have some suggestions to it.
1. I would like to get a branch of the tree from $lft up to the root.
2. Argh, just slipped my mind. ^^
Again N I C E work! =)
Posted: 01 August 2008 06:07 PM
[ Ignore ]
[ # 35 ]
[ Rating: 0 ]
Joined: 2006-08-03 671 posts
That error is fixed in 0.1.6-fix2, which you can find here
If you want a branch from lft to root, use get_parents(lft, rgt).
EDITED just after post: I added the answer to the branch question.
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