CakePHP 3 - 查询生成器正确地不适用于 HasMany 关联
CakePHP 3 - Query builder properly not working for HasMany associations
Table : Services
+-----+--------------+
| id | title |
+-----+--------------+
| 1 | Service 1 |
+-----+--------------+
| 2 | Service 2 |
+-----+--------------+
Table : Workshops { HasMany WorkshopServices
}
+-----+---------------+
| id | title |
+-----+---------------+
| 1 | Workshop 1 |
+-----+---------------+
| 2 | Workshop 2 |
+-----+---------------+
Table : WorkshopServices
+-----+--------------+-------------+
| id | workshop_id | service_id |
+-----+--------------+-------------+
| 1 | 1 | 1 |
+-----+--------------+-------------+
| 2 | 1 | 2 |
+-----+--------------+-------------+
我想通过 service_id
找到 Workshops
My Query
$this->Workshops->find()
->contain([
'WorkshopServices'
])
->where([
'WorkshopServices.service_id IN'=>[1,2,3]
]);
Query Result
Unknown column `WorkshopServices.service_id` in 'where clause'
实际上 Workshops
table 没有生成任何 JOIN
和 WorkshopServices
table.
如何编写查询以从 Query Builder
获得正确的结果?
使用匹配:
$array = [1,2,3];
$this->Workshops->find()
->matching('WorkshopServices', function ($q) use($array) {
return $q->where(['WorkshopServices.service_id IN' => $array])
});
I updated @GabrielFerreira's query and Grouping the rows by WorkshopServices.workshop_id
, This solution meet my Problem
$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']);
Table : Services
+-----+--------------+
| id | title |
+-----+--------------+
| 1 | Service 1 |
+-----+--------------+
| 2 | Service 2 |
+-----+--------------+
Table : Workshops { HasMany
WorkshopServices
}
+-----+---------------+
| id | title |
+-----+---------------+
| 1 | Workshop 1 |
+-----+---------------+
| 2 | Workshop 2 |
+-----+---------------+
Table : WorkshopServices
+-----+--------------+-------------+
| id | workshop_id | service_id |
+-----+--------------+-------------+
| 1 | 1 | 1 |
+-----+--------------+-------------+
| 2 | 1 | 2 |
+-----+--------------+-------------+
我想通过 service_id
Workshops
My Query
$this->Workshops->find()
->contain([
'WorkshopServices'
])
->where([
'WorkshopServices.service_id IN'=>[1,2,3]
]);
Query Result
Unknown column `WorkshopServices.service_id` in 'where clause'
实际上 Workshops
table 没有生成任何 JOIN
和 WorkshopServices
table.
如何编写查询以从 Query Builder
获得正确的结果?
使用匹配:
$array = [1,2,3];
$this->Workshops->find()
->matching('WorkshopServices', function ($q) use($array) {
return $q->where(['WorkshopServices.service_id IN' => $array])
});
I updated @GabrielFerreira's query and Grouping the rows by
WorkshopServices.workshop_id
, This solution meet my Problem
$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']);