EllisLab text mark
Advanced Search
     
Mahana Hierarchy library
Posted: 24 September 2012 02:14 AM   [ Ignore ]
Joined: 2009-04-15
453 posts

A small library based on the technique outlined here: http://ferdychristant.com/blog//archive/DOMM-7QJPM7

Stores & retrieves parent/child data without having to do recursive db hits. Comments in library file are pretty self-explanatory.

Output can either come in the form:

Record 1
Record 1.1
Record 1.2
Record 1.2.3
Record 2
...

or as a multi-dimensional array

https://github.com/jrmadsen67/mahana-hierarchy

All comments or recommendations welcomed.

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 21 October 2012 08:38 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts

Often threatened, rarely made good on…

a short tut on how to use this to create nested comments, etc without needing recursive functions:

http://codebyjeff.com/blog/2012/10/nested-data-with-mahana-hierarchy-library

library has a couple of additions to make it a little easier to work with, as well

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 22 October 2012 10:44 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-06-17
163 posts

I’ll use this library in a recent project for a welfare organization. Thanks a lot!

 
Posted: 22 October 2012 11:11 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-13
77 posts

I had a little time this morning, I took the opportunity to have a look there as I had said.
However, I have a question.
After multiple answers, I get this lineage:

00001-00002-00003-00006-00011-00012-00013-00014-00015-00016-00017 

Why not just (less in db):

1-2-3-5-11-12-13-14-15-16-17 

Otherwise, it happens when you post the 100000th post?

 

 

 
Posted: 22 October 2012 12:42 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2010-06-17
163 posts

For forums and CMS’s you can try this too (I’m not really sure about the performance, but I guess it’s good since they use it in Prestashop):

http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

 
Posted: 22 October 2012 06:08 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts
Noobigniter - 22 October 2012 11:11 AM

I had a little time this morning, I took the opportunity to have a look there as I had said.
However, I have a question.
After multiple answers, I get this lineage:

00001-00002-00003-00006-00011-00012-00013-00014-00015-00016-00017 

Why not just (less in db):

1-2-3-5-11-12-13-14-15-16-17 

Otherwise, it happens when you post the 100000th post?

 

the trick is to do a simple orderby on the lineage - if you don’t pad, you get

1-11-111-2-...

 

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 22 October 2012 06:16 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts
NeoArc - 22 October 2012 10:44 AM

I’ll use this library in a recent project for a welfare organization. Thanks a lot!

cool! let me know if you run into difficulties or have suggestions after using it

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 24 October 2012 02:26 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-13
77 posts

A suggestion, I do not know if is is one, but I immediately changed one little thing in the class, so you can use it on several tables (pages, category, comments, etc ...)

I commented :

//protected $_table = 'hierarchy'; 

and added this

public function __construct($_table '')
{
    $this
->_table $_table

Then it only remains to choose the table to use.

$this->test = new Mahana_hierarchy('comments');
$comments $this->test->get(0); 

It’s not much, but pretty handy.

Otherwise, nothing to report at this time.

PS: thank you

 
Posted: 24 October 2012 05:09 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts

yes, not a bad idea. I think I might actually add a full $config in instead, so later we can config other things as well (like padding size, for example)

glad it’s working out grin

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 25 October 2012 03:11 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-13
77 posts

Me again ^^
I noticed that it is easy to work on 1 table, however, and after creating a function like

get_where($top_id 0$where false$join false

, working on several tables becomes “dangerous “, even impossible if multiple identical fields in different tables (ie: id, user_id,..)
So I have to abandon much of the library, unfortunately.
I’ll just use the system of “lineage” on insert, which I had not thought of.

 
Posted: 25 October 2012 04:52 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts

yes, i think that’s why a config will be the better way to go.

I’ll have a look at adding that today if I get time

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 28 October 2012 02:45 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts

Couple quick updates:

1) now uses $config and an initialize method, so you can use multiple instances in site or even same page

2) composer package available: https://packagist.org/packages/jrmadsen67/mahana-hierarchy

Careful - I’ve renamed $_db to $db & $_table to $table as you can now pass these as $config keys

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 02 November 2012 05:47 AM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts

And a couple more grin

(I’m actively using this in a site I’m working on, so adding things as I find I need them)


get_ancestors($id, $remove_this = false)

Returns an array of all the ancestors of the given record. Second parameter will leave out the requesting record or not

get_parent($id)

Returns an row array of the parent of the given record.

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?