How to choose the fields from a associated model at find

85 Views Asked by At

Before I had this:

//ArticlesController::index
$articles = $this->Articles->find('all', [
  'contain' => ['Comments']
]);

So I set the fields key:

//ArticlesController::index
$articles = $this->Articles->find('all', [
  'fields' => ['title', 'text],
  'contain' => ['Comments']
]);

Since I set the fields key the result of the find is not bringing the comments anymore.

1

There are 1 best solutions below

0
On BEST ANSWER
$articles = $this->Articles->find('all')
        ->select(['fields_you_want_from_Articles'])
        ->contain(['Comments'  => function($q) {
            return $q
                ->select(['fields_you_want_from_Comments']);
        }]);