How to limit contained associations per record/group?
I have a Model, Articles, which hasMany Abstracts. I want to load the 10 latest Articles, and for each Article, the Abstract with the highest number of points. My function looks like this: public function getArticles($category, $viewName) { $subArticles = $this->Articles->findByCategory($category)->contain([ 'Abstracts' => function ($q) { return $q ->select(['body', 'points', 'article_id']) ->where(['Abstracts.approved' => true]) ->limit(10) ->order(['Abstracts.points' => 'DESC']); } ]) ->limit(10) ->order(['Articles.created' => 'DESC']) ; $this->set( $viewName . 'Articles', $subArticles ); }