Issue
When getting a single record, and associated records (two levels down), is there any way we can pass condition to the contained records in CakePHP 3? In this case, for example:
$user = $this->Users->get($this->Auth->user('id'), [
'contain' => [
'Articles' => ['Comments']
]
]);
If tried a bunch of ways with callbacks, but that only seems to work on the first level. Like so, for example:
$user = $this->Users->get($this->Auth->user('id'), [
'contain' => [
'Articles' => function ($q) {
return $q->where(['Articles.published' => true]);
}
]
]);
Any thoughts?
Solution
you should be able to do:
$user = $this->Users->get($this->Auth->user('id'), [
'contain' => [
'Articles' => function ($q) {
return $q->where(['Articles.published' => true]);
},
'Articles.Comments' => function ($q) {
return $q->where(['Comments.deleted' => false]);
}
]
]);
or
$user = $this->Users->get($this->Auth->user('id'), [
'contain' => [
'Articles' => function ($q) {
$q->contain([
'Comments' => function ($q) {
return $q->where(['Comments.deleted' => false]);
}
]);
return $q->where(['Articles.published' => true]);
}
]
]);
Answered By - arilia
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.