【问题标题】:Count in contain Cakephp 3计数包含 Cakephp 3
【发布时间】:2016-04-26 23:18:58
【问题描述】:

我有一张 Post 表格,它与表格 Starshas-many 关联。

我可以使用以下方法获取所有相关数据:

$this->Posts->find()->contain(['Stars']);

效果很好。

但我想数星星。我已经尝试过了,但它不起作用:

$this->Posts->find->contain([
    'Stars' => function($q) {
        return $q->select(['total' => $q->func()->count('Stars.post_id')]);
    }
]);

//I've also tried this
...
...$q->select(['total' => "COUNT(Stars.post_id)"]);
...
//Also fail

这不会返回关联的星数。

有什么不对还是应该以其他方式处理?

谢谢

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    您还必须选择外键,否则蛋糕无法加入表格。您还必须对结果进行分组

    'Stars' => function($q) {
        $q->select([
             'Stars.post_id',
             'total' => $q->func()->count('Stars.post_id')
        ])
        ->group(['Stars.post_id']);
    
        return $q;
    }
    

    【讨论】:

    • ) 函数中缺少 ) 大括号。请更新它。 @arilia
    • @SumonSarker 我编辑了,但你也可以根据需要编辑答案
    【解决方案2】:

    因为这里我们使用total作为虚拟字段,所以可以在相同的模型中创建更多这样的:

     public function index()
        {
            $checklist = TableRegistry::get('Checklists');
            $query = $checklist->find()
                ->where('Checklists.is_deleted = 0')
                ->contain([
                    'ChecklistTitles' => function($q) {
                        return $q -> select([
                            'ChecklistTitles.title',
                            'ChecklistTitles.checklist_id'
                    ]);
    
                },
                  'ChecklistTypes' => function($w){
                      return $w->select(['ChecklistTypes.type']);
                },
                  'AssignedChecklists' => function($e){
                                $e->select([
                                    'AssignedChecklists.checklist_id',
                                    'completed' => $e->func()
                        ->count('AssignedChecklists.checklist_id'),
                            ])
                            ->group(['AssignedChecklists.checklist_id'])
                            ->where(['AssignedChecklists.is_deleted = 0 AND AssignedChecklists.checklist_status = 2']);
                        return $e;
                    }
                    ]);
            // ->toArray();  
            // pr($query);die;   
                   $this->paginate = [
                'limit' => 20,
                'sortWhitelist' => [
                    'id', 'checklist_title', 'checklist_type'
                ]
            ];
            $this->set('query', $this->paginate($query));
            $this->set(compact('checklists','query'));
            $this->set('_serialize', ['checklists','query']);
    
        }
    

    到这里我已经计算完成了,我想用不同的 where 条件计算取消,在 cakephp3 中它的语法是什么?

    【讨论】:

      【解决方案3】:

      试试这个:

      $total = $this->Posts->find()->contain(['Stars'])->count();
      

      正如cookbook 中所提到的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-09
        • 2017-03-09
        • 2015-06-25
        • 2016-11-23
        • 2016-01-05
        • 2015-09-29
        • 1970-01-01
        相关资源
        最近更新 更多