Paginator for cakephp5. x. The URL on the next page has removed fields with empty values

  1. cakephp5.x>users
    UsersController.php
protected array $paginate = [
        'limit' => 5,
    ];

    /**
     * Index method
     *
     * @return \Cake\Http\Response|null|void Renders view
     */
    public function index()
    {
        $query = $this->Users->find();
        $users = $this->paginate($query);

        $this->set(compact('users'));
    }

…/templates/Users/index.php

<!--enquiry form -->
<?= $this->Form->create(null, ['type' => 'get']); ?>
        <?= $this->Form->control('email', [
            'label' => '邮箱:',
            'type' => 'text',
            'default' => '',
            'value' => $this->getRequest()->getQuery('email'),
        ]); ?>
        <?= $this->Form->button('检索'); ?>
    <?= $this->Form->end(); ?>

...
<!-- page -->
<div class="paginator">
        <ul class="pagination">
            <?= $this->Paginator->first('<< ' . __('first')) ?>
            <?= $this->Paginator->prev('< ' . __('previous')) ?>

            <?= $this->Paginator->counter() ?>
            
            <?= $this->Paginator->next(__('next') . ' >', ) ?>
            <?= $this->Paginator->last(__('last') . ' >>') ?>
        </ul>
        <p><?= $this->Paginator->counter(__('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')) ?></p>
    </div>

Problem description:
检索>btn 》url
http://localhost:8765/users?email=

$this->Paginator->next() 》 url
http://localhost:8765/users?page=2

When the condition ‘email’ is null, the pagination button removes it.

Hope to keep null values
http://localhost:8765/users?email=&page=2

You can write your own PaginatorHelper that extends core and overwrite

public function options(array $options = []): void

That one uses array_filter() to remove emptyish values, and as such empty strings are removed.

An easier way would be to use replacement values for empty here.
For numeric positive ones I often use 0 or -1 as empty value.
In your case you could use it too - or even just - or alike:
/users?email=-&page=2
The Search plugin can handle those values and keep them in the URL as well as transform them into the actual NULL meaning for your filtering.

Not to confuse with search/src/Controller/Component/SearchComponent.php at 6f905a2205fc2633204952c245dff9692448f488 · FriendsOfCake/search · GitHub
As those are the opposite.

More like: You build your filters and then make sure that “empty value” transforms back to NULL for the actual query.

1 Like

Thank you very much for your answer

See also Allow more easy "empty value" search · Issue #351 · FriendsOfCake/search · GitHub for further actions possibly to be taken here either in core or in plugin behavior.
For now the workaround should be working fine at least.