如何在查找时从关联模型中选择字段
How to choose the fields from a associated model at find
在我拥有这个之前:
//ArticlesController::index
$articles = $this->Articles->find('all', [
'contain' => ['Comments']
]);
所以我设置 fields key
:
//ArticlesController::index
$articles = $this->Articles->find('all', [
'fields' => ['title', 'text],
'contain' => ['Comments']
]);
由于我设置了 fields key
,find
的结果不再带来 comments
。
$articles = $this->Articles->find('all')
->select(['fields_you_want_from_Articles'])
->contain(['Comments' => function($q) {
return $q
->select(['fields_you_want_from_Comments']);
}]);
在我拥有这个之前:
//ArticlesController::index
$articles = $this->Articles->find('all', [
'contain' => ['Comments']
]);
所以我设置 fields key
:
//ArticlesController::index
$articles = $this->Articles->find('all', [
'fields' => ['title', 'text],
'contain' => ['Comments']
]);
由于我设置了 fields key
,find
的结果不再带来 comments
。
$articles = $this->Articles->find('all')
->select(['fields_you_want_from_Articles'])
->contain(['Comments' => function($q) {
return $q
->select(['fields_you_want_from_Comments']);
}]);