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