【发布时间】:2020-12-17 16:39:01
【问题描述】:
我有三个模型,Companies、events 和 assistances,其中 assistances 表存储 event_id 和company_id。我想获得一个查询,其中存储了公司对某种事件的总援助。然而,由于所有这些计数都链接到同一个表,我真的不知道如何有效地构建这个查询。我将每种事件的辅助 ID 存储在一些数组中,然后执行以下操作:
$query = $this->Companies->find('all')->where($conditions)->order(['name' => 'ASC']);
$query
->select(['total_assistances' => $query->func()->count('DISTINCT(Assistances.id)')])
->leftJoinWith('Assistances')
->group(['Companies.id'])
->autoFields(true);
尽管如此,我不知道如何获得其余的协助计数,因为我不需要计算所有不同的协助 ID,而只需要计算那些适合特定条件的 ID,例如 ->select(['assistances_conferences' => $query->func()->count('DISTINCT(Assistances.id)')])->where($conferencesConditions)(但显然上一行不起作用。有没有办法在查询本身中计算不同类型的帮助?(我需要这样做,因为我计划使用分页并考虑这些字段对表格进行排序)。
【问题讨论】:
标签: php join cakephp conditional-statements cakephp-3.x