# Limiting Association inside get function

**URL:** https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323
**Category:** Uncategorized
**Created:** [July 7, 2023, 1:37pm UTC](https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323 "2023-07-07T13:37:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Lamine](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@Lamine](https://discourse.cakephp.org/u/Lamine)
#### Post date: [July 7, 2023, 1:37pm UTC](https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323/1 "2023-07-07T13:37:03Z")

</div>

Any way to limit associations inside get function?

$user = $this-\>Users-\>get($id, [  
‘contain’ =\> [‘Articles’],  
‘limit’ =\> 5 // isn’t working  
]);

---

<div class="post-metadata">

### Author: ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)
#### Post date: [July 7, 2023, 4:44pm UTC](https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323/2 "2023-07-07T16:44:18Z")

</div>

See [cakephp - How to limit contained associations per record/group? - Stack Overflow](https://stackoverflow.com/questions/30241975/how-to-limit-contained-associations-per-record-group)  
and [cakephp - Using limit() on contained model - Stack Overflow](https://stackoverflow.com/questions/29775766/using-limit-on-contained-model)

---

<div class="post-metadata">

### Author: ![Lamine](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@Lamine](https://discourse.cakephp.org/u/Lamine)
#### Post date: [July 7, 2023, 8:53pm UTC](https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323/3 "2023-07-07T20:53:02Z")

</div>

Thanks but those solutions didn’t work. Plus some of those are CakePHP 2 version.

---

<div class="post-metadata">

### Author: ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)
#### Post date: [July 8, 2023, 3:39am UTC](https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323/4 "2023-07-08T03:39:50Z")

</div>

The first link given is pretty much the canonical answer to this question, so if it didn’t work, it seems likely that it’s a problem with how you’ve implemented it. Share what you ended up with by following that answer and maybe we can spot where you’ve gone wrong?

---

<div class="post-metadata">

### Author: ![Lamine](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@Lamine](https://discourse.cakephp.org/u/Lamine)
#### Post date: [July 8, 2023, 6:55am UTC](https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323/5 "2023-07-08T06:55:33Z")

</div>

![Screenshot (52)](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/1/11a0347c704761663f7d1c302ae8493732e54cf1.png)  
 ![Screenshot (53)](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/6/6b5765521ff9a478829520deb9ed2bc331ff0387.png)

In Users::view action. I’m trying to load the assocciated articles and limit them to only 5 records.

---

<div class="post-metadata">

### Author: ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)
#### Post date: [July 9, 2023, 6:54pm UTC](https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323/6 "2023-07-09T18:54:40Z")

</div>

There’s no reason why what you’ve tried would work. What I _think_ you’re trying to do is

```auto
'contain' => ['Articles' => ['limit' => 5]]

```

but that’s not supported. In the first link that Kevin gave, ndm has provided 6 possible solutions for this. What you’ve written here is nothing like any of them.

---

<div class="post-metadata">

### Author: ![Lamine](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@Lamine](https://discourse.cakephp.org/u/Lamine)
#### Post date: [July 9, 2023, 7:41pm UTC](https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323/7 "2023-07-09T19:41:44Z")

</div>

Yeah at first I tried:

‘contain’ =\> [‘Articles’ =\> [‘limit’ =\> 5]]. But it didn’t work. Going back to the docs get() function has ‘limit’ method and that’s exactly what I was trying in the code you see.

What ndm provided for hasMany scenario is a library solution

$this-\>hasMany(‘associatedTable’)  
-\>setLimit(5);  
And I don’t want to add that library to my project to only achieve this goal.

Any solution I tried and didn’t work I remove it that’s why the code is nothing like what ndm has provided.

---

<div class="post-metadata">

### Author: ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)
#### Post date: [July 10, 2023, 10:34pm UTC](https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323/8 "2023-07-10T22:34:41Z")

</div>

Keep reading there. He provided a library solution, two different strategies using select, one using join, one using window functions, and one with a bit of a brute force method. If none of these options are acceptable to you, then you’re probably going to need to submit a PR to Cake which adds direct support for the functionality you want, and hope it gets accepted.

---

<div class="post-metadata">

### Author: ![dipakmewada](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@dipakmewada](https://discourse.cakephp.org/u/dipakmewada)
#### Post date: [July 23, 2023, 5:46am UTC](https://discourse.cakephp.org/t/limiting-association-inside-get-function/11323/9 "2023-07-23T05:46:50Z")

</div>

Hi @Lamine ,

You can try using the strategy\_select option.

[https://api.cakephp.org/4.4/class-Cake.ORM.Association.html#STRATEGY\_SELECT](https://api.cakephp.org/4.4/class-Cake.ORM.Association.html#STRATEGY_SELECT)
