zoukankan      html  css  js  c++  java
  • 评论列表优化联表问题

    public function read1()
    {
        $newsId = $this->request->param('news_id', 0, 'intval');
        $param = $this->request->param();
        $where['news_id'] = ['=', $newsId];
        $where['status'] = ['=', 1];
        $count = model('Comment')->getNormalCommentsCount($where);
        $this->getPageAndSize($param);
        $comments = model('Comment')->getNormalComments($where, $this->from, $this->size);
        if( !$comments->isEmpty() ) {
            $userId = [];
            foreach ($comments as $comment) {
                $userId[] = $comment['user_id'];
                if( !empty($comment['to_user_id']) ) {
                    $userId[] = $comment['to_user_id'];
                }
            }
            $ids = array_unique($userId);
            $condition['id'] = ['in', $ids];
            $users = model('User')->getUserInId($condition);
            if( empty($users) ) {
                $userData = [];
            } else {
                foreach ($users as $user) {
                    $userData[$user['id']] = $user;
                }
            }
            $returnData = [];
            foreach ($comments as $comment) {
                $returnData[] = [
                    'id' => $comment['id'],
                    'content' => $comment['content'],
                    'user_id' => $comment['user_id'],
                    'news_id' => $comment['news_id'],
                    'parent_id' => $comment['parent_id'],
                    'create_time' => $comment['create_time'],
                    'username' => empty($userData[$comment['user_id']]) ? "" : $userData[$comment['user_id']]['username'],
                    'toUsername' => empty($userData[$comment['to_user_id']]) ? "" : $userData[$comment['to_user_id']]['username'],
                    'userImage' => empty($userData[$comment['user_id']]) ? "" : $userData[$comment['user_id']]['image'],
                ];
            }
            $total_page = ceil($count / $this->size);
            $result = [
                'total_page' => $total_page,
                'page' => $this->page,
                'list' => $returnData,
            ];
        } else {
            $result = [];
        }
        return Show::success($result);
    }
  • 相关阅读:
    [LeetCode]24. Search Insert Position插入位置
    [LeetCode]23. Set Matrix Zeroes矩阵清零
    [LeetCode]22. 3Sum Closest最近三者之和
    [LeetCode]21. 3Sum三者之和
    [LeetCode]20. Unique Paths II唯一路径
    [LeetCode]19. Unique Paths唯一路径
    [LeetCode]18. Contains Duplicate II重复检测
    [LeetCode]17. Majority Element主元素
    第13章建造者模式
    第15章抽象工厂模式
  • 原文地址:https://www.cnblogs.com/aln0825/p/13488353.html
Copyright © 2011-2022 走看看