Hey, is it also possible to create a Pagination for Comments that belong to an Article?
right now they can be accessed like:
$article->comments
and i do:
<?php foreach ($article->comments as $key => $comment) { ?>
but if there are hundrets of Comments that will not work anymore…
how can i limit this pagination to 5 for example?
$comments = $this->paginate($this->Comments
->find()
->contain([‘Users’, ‘Users.Profiles’])
->limit(‘5’)
->where([‘article_id =’ => $id]))
because this is not working.
here it is working: $this->set(‘articles’,$this->paginate($articles,[‘limit’=>‘3’]));
but its a different syntax…
Zuluru
September 26, 2021, 4:23am
4
In your first example, you’re passing a query in to paginate
, which is overriding the limit with its own default. In the second case, you’re explicitly telling paginate
what the limit should be. So:
$query = $this->Comments
->find()
->contain([‘Users’, ‘Users.Profiles’])
->where([‘article_id =’ => $id]);
$comments = $this->paginate($query, ['limit' => 5]);
should work as expected. (You can do it all without the $query
variable; I separated it to make it clearer what is doing what.)