【问题标题】:CakePhp 3 - How to order by SUM in contain resultCakePhp 3 - 如何在包含结果中按 SUM 排序
【发布时间】:2015-08-24 10:42:13
【问题描述】:

我正在从事 CakePhp 3 项目,我需要按每条评论的投票总和对文章中的 cmets 进行排序。

型号:

  • 文章
  • 文章评论
  • 文章评论投票
  • 用户

用户与每个模型相关联。 ArticlesComments 具有文章 ID。 ArticlesCommentsVotes 有comment_id 和user_id。

我需要什么:

  • 阳性 cmets 总数
  • 负 cmets 总数
  • 按阳性 cmets 号排序
  • 按负数排序

不知何故,我设法得到了 Total no。正和负 cmets,但 CakePhp 不允许我按 cmets 的数量排序。

这是我的查询:

$article = $this->Articles->get($id, [
            'contain' => [
                'Categories',
                'Clusters',
                'Tags',
                'ArticlesSteps',
                'PositiveVotes'    => function ($a){
                    return $a->select(['PositiveVotes.article_id', 'votes' => 'COUNT(*)']);
                },
                'NegativeVotes'    => function ($a){
                    return $a->select(['NegativeVotes.article_id', 'votes' => 'COUNT(*)']);
                },
                'ArticlesComments' => function ($q){
                    return $q->contain([
                        'Users'                 => function ($q){
                            return $q->select(['username']);
                        },
                        'CommentVote'           => function ($q){
                            return $q->select(['vote']);
                        },
                        'CommentsPositiveVotes' => function ($q){
                            return $q->select(['CommentsPositiveVotes.comment_id', 'positive' => 'SUM(vote)'])->group('comment_id');
                        },
                        'CommentsNegativeVotes' => function ($a){
                            return $a->select(['CommentsNegativeVotes.comment_id', 'CommentsNegativeVotes.user_id']);
                        }
                    ]);
                }
            ]
        ]);

任何帮助表示赞赏:)

【问题讨论】:

    标签: php database cakephp associations cakephp-3.0


    【解决方案1】:

    我认为,您无法对查询结果进行排序。但在你的情况下,使用名为“CounterCache”的东西是有意义的。

    http://book.cakephp.org/3.0/en/orm/behaviors/counter-cache.html

    【讨论】:

    • 感谢您的回复
    【解决方案2】:

    我找到了修复它的方法。我需要使用左连接。这是我更新的代码:

           // Search comments
            $comments = $this->ArticlesComments->find()->where([
                'ArticlesComments.article_id' => $articleId,
            ])->contain([
                'CommentVote' => function ($q){
                    return $q->select(['vote']);
                },
                'Users'       => function ($q){
                    return $q->select(['username']);
                },
            ]);
    
            // Left Join with ArticlesComments
            $comments
                ->leftJoin(
                    ['ArticlesCommentsVotes' => 'articles_comments_votes'],
                    ['ArticlesComments.id = ArticlesCommentsVotes.comment_id']
                );
    
    
            // Case
            $ratingCases = $comments->newExpr()->addCase([
                $comments->newExpr()->add(['ArticlesCommentsVotes.vote' => '1']),
                $comments->newExpr()->add(['ArticlesCommentsVotes.vote' => '0'])
            ], [1, - 1, 0], ['integer','integer','integer']);
    
            // Calculate rating and sort
            $comments->select(['rating' => $comments->func()->sum($ratingCases)])
                     ->group('ArticlesCommentsVotes.comment_id')->order($sortBy . ' ' . $sortOrder)->autoFields(true);
    

    【讨论】:

      猜你喜欢
      • 2016-02-11
      • 2013-07-11
      • 1970-01-01
      • 2010-11-21
      • 2013-01-20
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 2019-12-26
      相关资源
      最近更新 更多