I created a comments system in my app which is now attached to my articles->view. One of the features of the comments controller is to be able to allow users to respond to a comment (one level) and being able to create many children comments for a particular comment. Everything is connected thru parent_id.
Right now, when I open an article I use foreach $comments as $comment to retrieve all comments related to the article and in articles_controller.php I assign the $comments variable to find all comments related to the article.
What is the best way to get the children comments of $comment and display it on my view?
The way I am approaching it is to have a function that returns a list of children comments based on $comments->id and display it under $comment per figure below… This seems to be time consuming as a call will be made to this function(ie. childrenComments($id)) for each comment and can definitely bog down the server.
Does anyone know of a better way to use the parent_id relation and save some CPU time?

thank you all for your help.
I could not figure out how to do this, maybe the right way, so I opted to use elements to accomplish this. Basically, at each iteration of
foreach $comments, I make a call tochildrenCommentselement to check if there are children comments for the current$comment['Comment']['id']. If there are comments, I use for each to display each comment below its parent comment. See some code below….Code in articles_controller.php
Element childrenComments.ctp
Code in my /articles/view.ctp
And the result is:
