# Cakephp 3 - limit () and contained model

**URL:** <https://discourse.cakephp.org/t/cakephp-3-limit-and-contained-model/794>\
**Category:** Need Help\
**Created:** [July 24, 2016, 8:29am UTC](https://discourse.cakephp.org/t/cakephp-3-limit-and-contained-model/794 "2016-07-24T08:29:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![lupy](https://avatars.discourse-cdn.com/v4/letter/l/9e8a1a/32.png) [@lupy](https://discourse.cakephp.org/u/lupy)\
**Post date:** [July 24, 2016, 8:29am UTC](https://discourse.cakephp.org/t/cakephp-3-limit-and-contained-model/794/1 "2016-07-24T08:29:35Z")

</div>

I have two models, named Post and Comment, which are linked by Post hasMany Comment and Comment belongsTo Post.  
I want to fetch all posts with the first five comments each. I would use this code:

> $this-\>Posts-\>find(‘all’)  
> -\>contain([  
> ‘Comments’ =\> function($q) {  
> return $q  
> -\>order(‘created ASC’)  
> -\>limit(5);  
> }  
> ]);

This works incorrectly with the limit(). Please help to solve this.  
I used this example:  
[How to limit contained associations per record/group?](http://stackoverflow.com/questions/30241975/how-to-limit-contained-associations-per-record-group)  
I tried to like this (in Post model):

> $this-\>hasOne(‘TopComments’, [  
> ‘className’ =\> ‘Comments’,  
> ‘strategy’ =\> ‘select’,  
> ‘conditions’ =\> function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) {  
> $query-\>leftJoin(  
> [‘CommentsFilter’ =\> ‘comments’],  
> [  
> ‘TopComments.post\_id = CommentsFilter.post\_id’,  
> ‘TopComments.created \< CommentsFilter.created’  
> ]);  
> return $exp-\>add([‘CommentsFilter.id IS NULL’]);  
> }  
> ]);

In Post controller:

> $this-\>Posts-\>find(‘all’)  
> -\>contain([  
> ‘TopComments’ =\> function($q) {  
> return $q  
> -\>order(‘TopComments.created ASC’)  
> -\>limit(5);  
> }  
> ]);

Unfortunately this does not work. I do not know where I’m wrong.
