CakePhp 3 - 如何在包含结果中按 SUM 排序

CakePhp 3 - How to order by SUM in contain result

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

型号:

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

我需要的:

不知怎么的,我总算拿到了总号。正面和负面评论,但 CakePhp 不允许我按评论数量排序。

这是我的查询:

$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']);
                        }
                    ]);
                }
            ]
        ]);

感谢任何帮助:)

我认为,您不能对查询结果进行排序。但在你的情况下,使用名为 "CounterCache".

的东西是有意义的

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

我找到了修复它的方法。我需要为此使用 left Join 。这是我更新的代码:

       // 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);