Paginator Error

Hello, I am moving to cake 5.1 and receive Paginator Error in the template inthe header tags.
My Controller function:

public function index($id = null) {
    $query = $this->News->find();

    $news = $this->paginate($query, [
        'scope' => 'news'
    ]);
    $this->set(compact('news'));
    $this->render('/Homepage/News/index');
}

Error: TypeError: Cake\View\View::get(): Argument #1 ($var) must be of type string, int given
called in PaginatorHelper.php on line 142
Template

<?php
/**
 * @var \\App\\View\\AppView $this
 * @var iterable<\\App\\Model\\Entity\\Article> $articles
 */
$yesno = $this->get('appYesNo');
$appnews = $this->get('appNews');
?>
<?= $this->Element('navigation', ['navi' => $prodItem]); ?>
<div class="news index content">
    <?= $this->Html->link(__('Neue Neuigkeit'), ['action' => 'add'], ['class' => 'button float-right']) ?>
    <h3><?= __('Neuigkeiten') ?></h3>
    <div class="table-responsive">
        <table>
            <thead>
                <tr>
                    <th width="40"><?= $this->Paginator->sort('id') ?></th>
                    <th width="300"><?= $this->Paginator->sort('title') ?></th>
                    <th><?= $this->Paginator->sort('type') ?></th>
                    <th><?= $this->Paginator->sort('folder') ?></th>
                    <th><?= $this->Paginator->sort('active') ?></th>
                    <th><?= $this->Paginator->sort('login') ?></th>
                    <th width="200" class="actions"><?= __('Actions') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($news as $news): ?>
                    <tr>
  <td><?= h($news->title) ?></td>
                    <td>
                        <?php
                        echo $appnews[$news->type];
                        ?>
                    </td>
                    <td><?= h($news->folder) ?></td>
                    <td>
                        <?php
                        echo $yesno[$news->active];
                        ?>
                    </td>
                    <td>
                        <?php
                        echo $yesno[$news->login];
                        ?>
                    </td>
                    <?= $this->Element('viewArticleAction', array('recCon' => 'News', 'action' => 'singleNews', 'recId' => $news->id, 'parentId' => $news->id)); ?>   
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="paginator">
        <ul class="pagination">
            <?= $this->Paginator->first('<< ' . __('first')) ?>
            <?= $this->Paginator->prev('< ' . __('previous')) ?>
            <?= $this->Paginator->numbers() ?>
            <?= $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>
</div>

Whats the problem? Kid regards Klaus

@Klaus first of all - please use the “Preformatted text” Feature in this Forum so your code actually gets pasted in properly. I manually cleaned it up for now.

But your “problem” here is this line at the top:

$appnews = $this->get('appNews');

You don’t need to retrieve data inside your templates. Whenever you set a variable inside the controller like so

$this->set(compact('news'));

you automatically have access to it via `$news`

Also you can look at all your accesible variables via Debug Kit “Variables” Panel or just add a simple

pr($this->viewVars);

in your template (and make sure your Debug Mode is active) to see what your current scope of variables has to offer.

You may write foreach($news as $item):

1 Like

Thanks, but looks to me as an bug. I have baked all controller, model and templates and without any changes from my side the error occures. It is cake 5.2.10. A short example:
public function userMembers($id = null) {

    $names = $this->request->getParam('pass');



    $query = $this->Users->find()

            ->where(\['member LIKE' => '%' . $names\[0\] . '%'\])

            ->order(\['username' => 'ASC'\]);

           

     $users = $this->paginate($query);        

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

    $this->set('group', $names);

}
        <thead>

            <tr>

                <th><?= $this->Paginator->sort('Users.id') ?></th>

                <th><?= $this->Paginator->sort('Users.username') ?></th>

                <th><?= $this->Paginator->sort('Users.phone') ?></th>

                <th><?= $this->Paginator->sort('Users.email') ?></th>

                <th><?= $this->Paginator->sort('Users.city') ?></th>

            </tr>

        </thead>

without the header, the template runs correct. Any suggestion?? Kinde regards klaus

Yes, that’s a bug in the code generation in case the plural/singular inflection leads to the same word.
A rare case, but sure an issue for “news”.
Fix singular/plural variable collision in baked templates by dereuromark · Pull Request #1075 · cakephp/bake · GitHub will fix it.

Thanks for a quick reply, is there an advice to fix it manually in the code?
I would not like to bake all templates from scratch.

Regards Klaus

It is only 1 place (index.php), with IDE this is 1 change to do. You could try custom bake templates and stuff, but this would probably be more work for until 1-2 days when the fix is released.

Hi Mark, because I am not used with this, I want to know, how I get the information about the fix and what my next step would be. Installation from scatch, baking all templates ???

Could you please give a short advice? Regards Klaus

If you are familiar with composer, you can also check out the dev branch in the meantime:
composer require statement: dev-fix-singular-plural-var-collision

This way you can use the fix already without it being released.

Release Bake 3.6.3 · cakephp/bake · GitHub Released the fix.