表:服务
+-----+--------------+
| id | title |
+-----+--------------+
| 1 | Service 1 |
+-----+--------------+
| 2 | Service 2 |
+-----+--------------+
表:研讨会 {HasMany
WorkshopServices
}
+-----+---------------+
| id | title |
+-----+---------------+
| 1 | Workshop 1 |
+-----+---------------+
| 2 | Workshop 2 |
+-----+---------------+
表: WorkshopServices
+-----+--------------+-------------+
| id | workshop_id | service_id |
+-----+--------------+-------------+
| 1 | 1 | 1 |
+-----+--------------+-------------+
| 2 | 1 | 2 |
+-----+--------------+-------------+
我希望Workshops
service_id
我的查询
$this->Workshops->find()
->contain([
'WorkshopServices'
])
->where([
'WorkshopServices.service_id IN'=>[1,2,3]
]);
查询结果
Unknown column `WorkshopServices.service_id` in 'where clause'
实际上Workshops
表格没有生成JOIN
个WorkshopServices
表格。
如何编写查询以从Query Builder
获得正确的结果?
答案 0 :(得分:2)
使用匹配:
$array = [1,2,3];
$this->Workshops->find()
->matching('WorkshopServices', function ($q) use($array) {
return $q->where(['WorkshopServices.service_id IN' => $array])
});
答案 1 :(得分:1)
我更新了@ GabrielFerreira的查询并按
WorkshopServices.workshop_id
对行进行分组,此解决方案符合我的问题
$array = [1,2,3];
$this->Workshops->find()
->select([
'title'=>'Workshops.title',
'id'=>'Workshops.id',
])
->matching('WorkshopServices', function ($q) use($array) {
return $q->where(['WorkshopServices.service_id IN' => $array]);
})
->group(['WorkshopServices.workshop_id']);