0
votes

I am creating a forum for my school. A student will be able to create many topics in a particular forum and other students are able to comment on it. The tricky part is that i have a feature where a student can have sub comment to a comment. Lemme tell you my simple table structure which i am using Mysql and developing in cakephp. Below is not the full structure.

User (id,name)
Forums(id,desc,date)
Topics(id,user_id,forum_id,title,content,date)
comments(id,user_id,topics_id,content,date,parent_id) *=> the parent_id referers where the subcomment belongs to.*

I used the cakePHP containable to get all toics and comments of a particular forum in my forum_controller:

$this->find('all',array(
            'contain'=>array(
                'User'=>array(
                    'fields' => array       ('id','displayName','gender','email','profileImgBig')
                 ),'Post'=>array(
                     'User'=>array(
                         'fields' => array('id','displayName','gender','email','profileImgBig')
                     ),
                     'order' => array('Post.created'=>'DESC'),
                 )
            ),
            'conditions'=>array('Topic.forum_id'=>$id),
            'order' => array('Topic.datePosted'=>'DESC')
       ));       

The above retrieves all forum->Topics->User & Topics->Comments->User.

How can i implement in a way that its retrieves all Comments and sub comments? Please help and advice me. Thank you! I am looking at tree behaviour but i am not sure how am i suppose to incorporate with my above find statement.

1
just curious, but why not modify a free, open source forum out there instead of writing one? - Anh Pham
I have not revealed the whole idea of the website that i am working on. The existing forums and the current forum that i am working have different concepts. - madi
Moreover, existinf forum codes are hard to follow and read unless you have a recommendation - madi

1 Answers

2
votes

You can indeed use the TreeBehaviour in CakePHP for this. You'll need to add two more fields to your database (lft, rght) besides parent_id for this and activate it in the model for comments.

See the CakePHP manual for details, but after activating the behaviour you can use functions like Model::children() and Model::find('threaded') to find all comments that reside below a certain comment (e.g, that are subcomments).

I would usually first query the database for all topics, and then queue the Comment model for comments and subcomments using the Model::children() function. I wonder though, why would you want to load all topics and all comments within those topics, and not just 1 topic with comments?