CakePHP 5 で "finder with options array is deprecated" が出る場合の対応方法
CakePHP 5 では find() を従来通り下記のように書くと deprecated エラーが出ます。
src/Controller/ArticlesController.php
// deprecated エラーが出るコード
$list = $this->Articles->find('list', [
'keyField' => 'id',
'valueField' => 'title',
])
->toArray();
logs/debug.log
2023-09-29 00:00:00 debug: Since 5.0.0: Calling `findList` finder with options array is deprecated. finder with options array is deprecated.
原因は第二引数でオプションを配列で渡していることで、解決するためには下記のように PHP 8 の名前付き引数を使います。
src/Controller/ArticlesController.php
$list = $this->Articles->find('list',
keyField: 'id',
valueField: 'title',
)
->toArray();
また第1引数も名前付き引数にして、下記のように書くこともできます。
src/Controller/ArticlesController.php
$list = $this->Articles->find(
type: 'list',
keyField: 'id',
valueField: 'title',
)
->toArray();
- ORM (Deprecations - 5.0 Migration Guide - 5.x)
- https://book.cakephp.org/5/en/appendices/5-0-migration-guide.html#id2
- 名前付き引数 (PHP: 関数の引数 - Manual)
- https://www.php.net/manual/ja/functions.arguments.php#functions.named-arguments
- find() (Class Table | CakePHP 5.0)
- https://api.cakephp.org/5.0/class-Cake.ORM.Table.html#find()
- findList() (Class Table | CakePHP 5.0)
- https://api.cakephp.org/5.0/class-Cake.ORM.Table.html#findList()