Error pagination hasMany results

Good night,

I’m updating my code to version 5 but I’m having a problem with the pager working.

Until now, I was looking for an entity (Ex. Companies) with other related entities of type hasMany (Ex. Workers), then I arrived at the pager, and no problem.

Now I look for the entity in the following way:

$company = $this->Companies->get($id, contain: ['Workers']);
$this->set(compact('company'));

And it gives me the following error “You must set a pagination instance using setPaginated() first”

I try to use the setPaginated method or with $this->paginate($company->workers); and it gives me this other error. I’m stuck and I don’t know how to continue.

I wonder if with the new version it is not possible to paginate an array of entities or I am not doing it correctly

Some help?

No, pagination does not work on arrays. That defeats much of the purpose, which is to decrease how many records are read from the database. Pagination only works on queries. So, instead of containing Workers, you’ll need to do a separate query to find the workers associated with the company, and paginate that.

First of all, thank you for your quick response. From my point of view, it’s a shame, because a lot of functionality is lost.
For me, but this is a personal opinion, the purpose of a pagination system is to paginate as many things as possible; one of the secondary objectives is to save processing time whenever possible.

Thanks a lot