升级cakephp 3.10到cakephp 4抛出Hash::insert()错误

33qvvth1  于 2023-11-19  发布在  PHP
关注(0)|答案(1)|浏览(196)

按照CakePHP手册中的所有迁移步骤,我在所有页面上都出现了错误500。日志告诉我:

> [Wed Jul 19 11:56:02.566953 2023] [proxy_fcgi:error] [pid 12776]
> [client 178.117.91.131:57762] AH01071: Got error 'PHP message: PHP
> Fatal error:  Uncaught TypeError: Argument 2 passed to
> Cake\\Utility\\Hash::insert() must be of the type string, int given,
> called in
> /var/www/clients/client0/web4/web/ID/vendor/cakephp/cakephp/src/Core/Configure.php
> on line 92 and defined in
> /var/www/clients/client0/web4/web/ID/vendor/cakephp/cakephp/src/Utility/Hash.php:316\nStack
> trace:\n#0
> /var/www/clients/client0/web4/web/ID/vendor/cakephp/cakephp/src/Core/Configure.php(92):
> Cake\\Utility\\Hash::insert()\n#1
> /var/www/clients/client0/web4/web/ID/vendor/cakephp/cakephp/src/Core/Configure.php(339):
> Cake\\Core\\Configure::write()\n#2
> /var/www/clients/client0/web4/web/ID/config/bootstrap.php(74):
> Cake\\Core\\Configure::load()\n#3
> /var/www/clients/client0/web4/web/ID/vendor/cakephp/cakephp/src/Http/BaseApplication.php(161):
> require_once('/var/www/client...')\n#4
> /var/www/clients/client0/web4/web/ID/src/Application.php(41):
> Cake\\Http\\BaseApplication->bootstrap()\n#5
> /var/www/clients/client0/web4/web/ID/vendor/cakephp/cakephp/src/Http/Server.php(10...'

字符串
由于我不知道--或者说看不到--在Application.php或bootstrap.php中哪里调用了哈希,所以我不确定如何调试它。

dnph8jn4

dnph8jn41#

php变得越来越严格。这不是CakePHP的问题。你的问题是基于传递一个整数给一个不再接受整数的函数。通常这些问题可以通过使用(string)转换为字符串来解决,比如:

$path = 3;
$values = 'your value';
Hash::insert($data, (string)$path, $values);

字符串
但在你的情况下,实际的问题是在你的bootstrap.php第74行,你调用Configure::load(),它调用Configure::write(),然后调用Hash::insert(),因此你的配置中的每个键(通常在config/app.php中)必须是字符串类型->在你的配置中,你必须有整数键或隐式整数键,通过跳过键(数字数组):

$config = [
    ['3' => 'your value'], // doesn't work because the actual key would be an automatic numeric array index (0 in this case)
    '3' => 'your value', // works
     (string)3 => 'your value', // works
     4 => 'your value', // doesn't work without warnings or errors
    '5' => ['3' => 'your value'], // works 
    '6' => [3 => 'your value'], // works (numeric array as value on second level) 
];

Configure::write($config);

相关问题