Limit hasMany Relationship

cakephp 3.9
In Categories Model :
$this->hasMany(‘LastArticles’, [
‘className’ => ‘Articles’,
‘foreignKey’ => ‘category_id’,
‘setStrategy’ => ‘select’,
‘queryBuilder’ => function ($q) {
return $q->limit(5);
}
]);
then in categoriesController :
$query = $this->Categories->find()
->select([‘Categories.id’, ‘Categories.name’, ‘Categories.parent_id’])
->contain([
‘ParentCategories’,
‘LastArticles’ => function ($q) {
return $q
->select([‘LastArticles.id’, ‘LastArticles.category_id’, ‘LastArticles.name’, ‘LastArticles.slug’])
->order([‘LastArticles.created’ => ‘DESC’])
;
}
])
->order([‘Categories.lft’ => ‘ASC’])
;

No error but no limit :frowning:
What’s wrong ?

Please use code blocks when you share your code. It makes more readable.

At debugkit’s SQL tab you can see the generated SQL statement. It can help you identify the problem.

I tried add 'limit' => 5 in model i my working app and it does nothing, then I put it into
'contain' => ['Detals' => ['limit' => 5] ] and it does eagerLoader error. My version is 3.1 and I can’t help you. It’s also hard to write it in SQL with simple one join.

RRD : Ok. Sorry. I just forgot … :frowning:

Thank you jarekgol for giving your solution.
But it does nor work in 3.9