How use Paginate Next inside a controller?

    $data = TableRegistry::get('Receivables')->find('all')->where(['Receivables.boleto_gerado' => 0])->contain([
    ])->matching('SaleDetails', function ($q) {
        return $q->where(['SaleDetails.nota_fiscal !=' => '']);

    $accounts = $this->getAccounts();
    $receivables = $this->paginate($data);

    while ($this->receivables->next) {
        foreach ($data as $datat) {

//stuff here

I tried doing while $this->receivables->next but it didnt work, how I can advance pagination in a while to do my stuff ? I’m doing this because I cant disable buffer and my query is really big

What are you expecting $this->receivables->next to be, especially given that you’ve put your query results into $receivables? What does foreach ($receivables as $receivable) not do that you need it to do?

@zuluru If I do foreach it will throw memory error. because the reuslts are very large
So my idea was to load the query inside a paginator and get the results 20 per 20…
or something like that, getting the best performance.

So my idea was to make a while is possible paginator->next
do foreach of the 20 results until it ends the whole query
like trying to process it in small batches, like u said in previous post :slight_smile:

->next is a function provided by the paginator helper, for use in views. It generates HTML output. It does not, by itself, give you any kind of database results. What are you trying to do here, generate a single page of output with more results on it than you can load with a single query, or generate multiple pages of results with pagination links between them, or do some periodic (daily/weekly/etc.) batch process on a large number of records?

1 Like

there is nice cakephp workshop vid on batch processing

1 Like

well, I’ve set a big maximium memory use, and it solved for now, because the query is big only on the first run, (after processed only new entries will be loaded).
I want is get output of all results but instead of loading them once and using alot of memory, load like 20 results then do a foreach and erase the previous variable, and load more 20 results… until there’s no results left on the query

thanks but bufferresult doesnt work in my case as I’ve seen a little of this video, its 1 hour of video in what minute there’s batch processing?

around 13min you have simple example, around 20min you have why your bufferResults doesnt work - spoiler: PHP fault
also sadly most of examples here uses bufferResults(false) so it might not work for you (still worth trying)

1 Like

Why doesn’t disabling buffering work for you? It is the correct solution for cases like this.

1 Like

I think it is because I have belongstoMany and other relationships… I disabled buffering and the same error of memory

Did you try my suggestion of using loadInto on a record-by-record basis instead of containing everything up front?

1 Like

Ah, yes, that can make things a bit tricky. @Zuluru’s suggestion of using loadInto is probably your best bet.

1 Like

//articles will have only article contents.
$articles = $this->Articles->find()->all();

//$withmore will have content of comments, and users.
$withMore = $this->Articles->loadInto($articles, [‘Comments’, ‘Users’]);


for example, my logic is right?

$articles = $this->Articles->find()->all();
foreach ($articles as $article) {
    $article = $this->Articles->loadInto($article, [‘Comments’, ‘Users’]);

You’ll be loading all the other entities into only one article at a time, cutting the memory requirements way down.