Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for a tree comment system with unlimited nesting?

I need to do a tree comment system for a highload Zend framework-based web-service with unlimited nesting, also this system must be very fast.

Comments must be sent for Zend_View in simple array such as bellow. In Zend_View whole comments will be displayed via foreach(). Structure will be shown with CSS classes.

array(
   '0' => 'comment1 (here must be a a much of data)',
   '1' => 'comment for comment1',
   '2' => 'comment2',
   '3' => 'comment3',
   '4' => 'comment for comment3',
   '5' => 'comment of comment ^_^ '
);

Now, I can receive all comments required page from model, which sorted by date. Their structure specified below.

id | page_id | user | parent | date | text & etc

id - AI primary key

page_id - TINY int

user - int

parent - TINY int

data - timestamp

like image 847
NiLL Avatar asked Dec 30 '25 11:12

NiLL


1 Answers

The simplest and least optimized

for unlimited nesting is a parent/child relationship on comment:

Comment
---
id_comment
id_parent ALLOW NULL
body
...etc

To output this you would have to first select all parents (comments with a parent of NULL), then have a recursive function to select each level of children for each parent and output it. Needless to say, this is way too much overhead.

I would suggest you look into the nested set model:

http://en.wikipedia.org/wiki/Nested_set_model

Using the nested set model you can select the entire tree with one query.

I haven't looked into this example too far, but maybe check this out: http://devzone.zend.com/1675/class-for-managing-nested-set-data/

like image 145
Francis Yaconiello Avatar answered Jan 01 '26 02:01

Francis Yaconiello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!