How to achieve second-level comments like a brief book

how to achieve second-level comments like a brief book?
my existing fields are as follows:

id  pid id article_id id

my original implementation is as follows

    /**
     * @param $data array  
     * @param $pid  string   parent_id
     * @param $id     string   comm_id
     * @param $p_id     int    id 
     * @return array
     */
    public function getSubTree($data , $pid , $id , $p_id = 0) {
        $tmp = array();
        foreach ($data as $key => $value) {
            $value["avatar"] = getAvatar($value["from_user_id"]);
            if($value[$pid] == $p_id) {
                $value["child"] =  $this->getSubTree($data , $pid , $id , $value[$id]);
                $tmp[] = $value;
            }
        }
        return $tmp;
    }

the comment structure obtained is as follows

clipboard.png

clipboard.png


$comments = Db::name('comment')->where($map)->limit($offset, $per_page)->select();
foreach ($comments as $k => $v){
    $comments[$k]['avatar'] = getAvatar($v['from_user_id']);
    $map['pid'] = $v['id'];
    $comments[$k]['child'] = Db::name('comment')->where($map)->select();
}

I can actually do this in this way, but what I am worried about is whether there will be performance problems when the amount of data is large. I hope experienced bosses can advise me.
here is the comment data structure I got in this way

clipboard.png


it is best not to block the large amount of sql, data in foreach
check out the article, then check out the comment, and then put it together


A company that I worked for before has done an APP. It is also the case of such second-level comments as you mentioned. In view of this situation, the solution is to divide it into two tables.
A table stores first-level comments. Another table stores secondary comments.
then query the first-level comment table each time you get it, and then query the second-level comment table again.


add a top-level parent node field

Menu