【发布时间】:2014-12-19 16:48:41
【问题描述】:
我正在尝试使用 CakePHP 3.0 beta 3 构建一个(或多或少复杂的)查询。我有以下实体:
用户属于ToMany 组 任务 belongsToMany 用户(别名为“收件人”) 任务属于ToMany 组
这是一个任务管理系统。任务可以发给特定用户,也可以发给指定的用户组。
我现在正在尝试查找属于当前用户或当前用户所属组的任务(与特定用户相关的所有任务)。我得到了用户 ID 和用户所属的组列表。
我已经设法使用以下代码使其工作:
$queryOptions = [
'contain' => [
'Recipients', 'Groups'
]
];
$tasksForUser = $this->Tasks->find('all', $queryOptions)->matching('Recipients', function ($q) use ($user_id) {
return $q->where(['Recipients.id' => $user_id]);
})->toArray();
$tasksForGroup = $this->Tasks->find('all', $queryOptions)->matching('Groups', function($q) use ($group_ids) {
return $q->where(['Groups.id IN' => $group_ids]);
})->toArray();
$this->set('tasks', array_merge($tasksForUser, $tasksForGroup));
CakePHP 3.0 声称有一个复杂的 ORM 模型,那么有没有办法在单个查询中执行它?它可能使我能够即时过滤掉重复项。
提前致谢, 戴夫
【问题讨论】:
标签: cakephp orm cakephp-3.0