0
0

More than 1 year has passed since last update.

CakePHP4のページネーションで「Argument #1 ($format) must be of type string, array given, called in」

Last updated at Posted at 2023-08-05

エラー内容

Cakeのページネーションで件数を表示しようとしたらエラーで動かない!
エラーメッセージには以下が表示されていました。

Cake\View\Helper\PaginatorHelper::counter(): Argument #1 ($format) must be of type string, array given, called in 

must be of type string, array given, called in
引数の型が違う?でもドキュメント通りのはず。

CakePHPのドキュメント

image.png
https://book.cakephp.org/4/ja/views/helpers/paginator.html

解決

<div class="list_num"><?php echo $this->Paginator->counter(['format' => "<span>{{count}}</span>件"]) ?></div>

を以下に書き換えて解決しました。

<div class="list_num"><?php echo $this->Paginator->counter("<span>{{count}}</span>件"); ?></div>

ドキュメントでは配列で渡してるのに、Stringで渡さないといけないのなぁぜなぁぜ?

仕様が変わってる。

/vendor/cakephp/cakephp/src/View/HElper/PaginatorHelper.php
のcounterが微妙に変わってました。
多分「counter('range')」とかで簡単に数字だけを呼び出せるようにって変更だとは思いますが、3からの移植を考えるとそのままでもよかったような気もします。optionが第二引数として残ってるのは移植用かな?。ページ関連とその他のオプションを明確に分けるのももしかしたらあるかもしれません。

CakePHP4
image.png

CakePHP3
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0