You must set a pagination instance using `setPaginated()` first

I need some advice with pagination in an application I migrated from 4.x to 5.x. I am getting the error message: “You must set a pagination instance using setPaginated() first” when my template file, index.php loads. The code snippet below is from my index.php file and is where the error occurs.

       <div class="paginator text-center">
            <ul class="pagination">
                <?= $this->Paginator->first('<< ' . __('First')) ?>
                <?= $this->Paginator->numbers() ?>
                <?= $this->Paginator->last(__('Last') . ' >>') ?>
            </ul>
            <p><?= $this->Paginator->counter() ?></p>
        </div>

In the controller, my code is as follows:

$beacons = $this->Beacons->find(‘all’, contain: [‘Triggers’, ‘Floors.Buildings.Sites’])->where([‘site_id’ => $site_id]);

$settings = [‘limit’ => 10, ‘scope’ => ‘beacons’, ‘order’ => [‘Beacons.name’ => ‘ASC’]];

$this->set(‘_serialize’, $this->paginate(‘beacons’, $settings));
$this->set(compact(‘beacons’));

All Im doing is showing a list of beacons(a beacon is a location for a wayfinder app), and trying to limit the view to 10 per page.

Has anyone ever had a similar issue when migrating to 5.x? If so, can you please share any suggestions/solutions?

Thanks in advance!

I ran into this issue multiple times.

In my case I passed down a non-paginated resultset/query from the controller to the view which i then tried to paginate and output table headers with sorting.

In your specific case I am very confused by these 2 lines:

$this->set(‘_serialize’, $this->paginate(‘beacons’, $settings));
$this->set(compact(‘beacons’));

you set the result of the paginate only in the _serialize key of the view but the beacons query is set direclty in the view by the 2nd call without pagination.

It should actually be

$beacons = $this->paginate('beacons', $settings);
$this->set(compact('beacons'));
$this->viewBuilder()->setOption('serialize', 'beacons');

because the _serialize special var key has been deprecated in CakePHP 4.0