# Limit hasMany Relationship

**URL:** https://discourse.cakephp.org/t/limit-hasmany-relationship/8770
**Category:** Need Help
**Created:** [December 23, 2020, 3:09pm UTC](https://discourse.cakephp.org/t/limit-hasmany-relationship/8770 "2020-12-23T15:09:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)
#### Post date: [December 23, 2020, 3:09pm UTC](https://discourse.cakephp.org/t/limit-hasmany-relationship/8770/1 "2020-12-23T15:09:55Z")

</div>

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 ☹  
What’s wrong ?

---

<div class="post-metadata">

### Author: ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)
#### Post date: [December 23, 2020, 6:50pm UTC](https://discourse.cakephp.org/t/limit-hasmany-relationship/8770/2 "2020-12-23T18:50:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)
#### Post date: [December 26, 2020, 9:12am UTC](https://discourse.cakephp.org/t/limit-hasmany-relationship/8770/3 "2020-12-26T09:12:28Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)
#### Post date: [December 26, 2020, 2:02pm UTC](https://discourse.cakephp.org/t/limit-hasmany-relationship/8770/4 "2020-12-26T14:02:51Z")

</div>

RRD : Ok. Sorry. I just forgot … ☹

---

<div class="post-metadata">

### Author: ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)
#### Post date: [December 31, 2020, 8:40pm UTC](https://discourse.cakephp.org/t/limit-hasmany-relationship/8770/5 "2020-12-31T20:40:24Z")

</div>

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