带有子模型的条件关联模型 CakePHP
Conditional associated models with children models CakePHP
我有以下数据库结构:
// Contacts
,----,------------,------------------,
| id | name | email |
|----|------------|------------------|
| 1 | John Doe | john@example.com |
'----'------------'------------------'
// Jobs
,----,------------,-------------------,
| id | title | description |
|----|------------|-------------------|
| 1 | Fancy Job | This job is fancy |
'----'------------'-------------------'
// JobRequests
,----,------------,------------,----------,
| id | job_id | contact_id | accepted |
|----|------------|------------|----------|
| 1 | 1 | 1 | yes |
| 2 | 1 | 2 | no |
'----'------------'------------'----------'
我想要所有已接受工作的联系人。
我已尝试执行以下操作:
$jobs = $this->Jobs
->find('all')
->where(['event_id' => $id])
->contain([
'JobRequests' => [
function(Query $query) {
return $query->where(['JobRequests.accepted' => 'yes']);
},
'Contacts'
]
]);
但是我收到了{{Unsupported operand types}}错误。如果你想要一个条件和一个包含,docs 并没有真正说明你应该做什么。
我该怎么做?
编辑:
我希望数据看起来像这样:
Job 1:
Accepted by John Doe
Accepted by Jane Doe
Job 2:
Accepted by Foo Bar
Job 3:
Nobody accepted
您使用了错误的 "syntax",您不能只在选项数组中传递一个可调用项。
在您链接的文档的下方,有一个示例如何嵌套查询构建器并传递附加条件... queryBuilder
选项:
->contain([
'JobRequests' => [
'queryBuilder' => function(Query $query) {
return $query->where(['JobRequests.accepted' => 'yes']);
},
'Contacts'
]
])
堆叠容器也应该有效:
->contain([
'JobRequests' => function(Query $query) {
return $query->where(['JobRequests.accepted' => 'yes']);
},
'JobRequests.Contacts'
])
嵌套也应该有效:
->contain([
'JobRequests' => function(Query $query) {
return $query
->where(['JobRequests.accepted' => 'yes'])
->contain(['Contacts']);
}
])
另见
我有以下数据库结构:
// Contacts
,----,------------,------------------,
| id | name | email |
|----|------------|------------------|
| 1 | John Doe | john@example.com |
'----'------------'------------------'
// Jobs
,----,------------,-------------------,
| id | title | description |
|----|------------|-------------------|
| 1 | Fancy Job | This job is fancy |
'----'------------'-------------------'
// JobRequests
,----,------------,------------,----------,
| id | job_id | contact_id | accepted |
|----|------------|------------|----------|
| 1 | 1 | 1 | yes |
| 2 | 1 | 2 | no |
'----'------------'------------'----------'
我想要所有已接受工作的联系人。
我已尝试执行以下操作:
$jobs = $this->Jobs
->find('all')
->where(['event_id' => $id])
->contain([
'JobRequests' => [
function(Query $query) {
return $query->where(['JobRequests.accepted' => 'yes']);
},
'Contacts'
]
]);
但是我收到了{{Unsupported operand types}}错误。如果你想要一个条件和一个包含,docs 并没有真正说明你应该做什么。
我该怎么做?
编辑: 我希望数据看起来像这样:
Job 1:
Accepted by John Doe
Accepted by Jane Doe
Job 2:
Accepted by Foo Bar
Job 3:
Nobody accepted
您使用了错误的 "syntax",您不能只在选项数组中传递一个可调用项。
在您链接的文档的下方,有一个示例如何嵌套查询构建器并传递附加条件... queryBuilder
选项:
->contain([
'JobRequests' => [
'queryBuilder' => function(Query $query) {
return $query->where(['JobRequests.accepted' => 'yes']);
},
'Contacts'
]
])
堆叠容器也应该有效:
->contain([
'JobRequests' => function(Query $query) {
return $query->where(['JobRequests.accepted' => 'yes']);
},
'JobRequests.Contacts'
])
嵌套也应该有效:
->contain([
'JobRequests' => function(Query $query) {
return $query
->where(['JobRequests.accepted' => 'yes'])
->contain(['Contacts']);
}
])
另见