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

CakePhp 3-如何在包含结果中按和排序,php,database,cakephp,associations,cakephp-3.0,Php,Database,Cakephp,Associations,Cakephp 3.0,我在CakePHP3项目上工作,我需要根据每条评论的投票总数对文章的评论进行排序 型号: 文章 条款建议 条款注释 使用者 用户与每个模型关联。ArticlesComments具有文章id。ArticlesCommentsVotes具有注释id和用户id 我需要的是: 正面评论总数 负面评论总数 按编号排序。正面评论 按负面评论数量排序 不知何故,我设法得到了肯定和否定评论的总数,但CakePhp不允许我按评论的数量排序 我的问题是: $article = $this->Artic

我在CakePHP3项目上工作,我需要根据每条评论的投票总数对文章的评论进行排序

型号:

  • 文章
  • 条款建议
  • 条款注释
  • 使用者
用户与每个模型关联。ArticlesComments具有文章id。ArticlesCommentsVotes具有注释id和用户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']);
                        }
                    ]);
                }
            ]
        ]);

非常感谢您的帮助:)

我想,您不能根据查询结果进行排序。但在您的情况下,使用称为“计数器缓存”的东西是有意义的


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

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

谢谢你的回复