简体   繁体   English

Codeigniter 4 Seeder 给出错误,“数组到字符串的转换”

[英]Codeigniter 4 Seeder gives an error, "Array to string conversion"

When I add dh_setting_context' => 'user1' I get an error of Array to string conversion , however, when I remove that line from the seeder, it works and the table is correctly populated.当我添加dh_setting_context' => 'user1'时,我收到Array to string conversion的错误,但是,当我从播种机中删除该行时,它可以工作并且表格已正确填充。 Why does dh_setting_context' => 'user1' generator an error and how to fix it, please?为什么dh_setting_context' => 'user1'产生错误,请问如何解决?

Here is my seeder file这是我的播种机文件

namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
use \CodeIgniter\I18n\Time;

class DhSettingSeeder extends Seeder
{

public function run()
{
    $data = [
        
        // app - General App Settings
        [
            'dh_setting_key'            => 'app.dh_company_name' ,
            'dh_setting_value'          => 'ABC Big Co., Ltd',
            'dh_setting_created_at'     => Time::now(),
            'dh_setting_updated_at'     => Time::now()
        ],
        
        [
            'dh_setting_key'            => 'app.dh_company_address_line_1' ,
            'dh_setting_value'          => '123 Long Street',
            'dh_setting_created_at'     => Time::now(),
            'dh_setting_updated_at'     => Time::now()
        ],
        
        
        // email - general settings for email
        [
            'dh_setting_key'            => 'email.fromEmail' ,
            'dh_setting_value'          => 'example@yahoo.com',
            'dh_setting_created_at'     => Time::now(),
            'dh_setting_updated_at'     => Time::now()
        ],
        [
            'dh_setting_key'            => 'email.SMTPHost' ,
            'dh_setting_value'          => 'smtp.mail.yahoo.com',
            'dh_setting_created_at'     => Time::now(),
            'dh_setting_updated_at'     => Time::now()
        ],
        // add some user settings for admin user (user id = 1)
        [
            'dh_setting_key'            => 'user.timezone' ,
            'dh_setting_value'          => 'gmt',
            'dh_setting_context'        => 'user1',
            'dh_setting_created_at'     => Time::now(),
            'dh_setting_updated_at'     => Time::now()
        ]
    ];
    $this->db->table('dh_setting')->insertBatch($data);
}
}

Here is the structure of my table这是我的表的结构

结构 dh_setting 表

Not all of the entries in $data have the same keys.并非$data中的所有条目都具有相同的键。

CodeIgniter takes the keys from the first entry ( 'dh_setting_key' , 'dh_setting_value' , 'dh_setting_created_at' , 'dh_setting_updated_at' ) and uses these keys to check all of the entries in $data . CodeIgniter 从第一个条目( 'dh_setting_key''dh_setting_value''dh_setting_created_at''dh_setting_updated_at' )中获取键,并使用这些键检查$data中的所有条目。

If the keys of an entry are the same, it creates a string with the entry's values.如果条目的键相同,它会创建一个包含条目值的字符串。

If the keys of an entry are different, it creates an empty array.如果条目的键不同,则会创建一个空数组。

For the INSERT query, CodeIgniter then tries to concatenate the value strings with the empty array, which is not allowed and gives the error.对于 INSERT 查询,CodeIgniter 然后尝试将值字符串与空数组连接起来,这是不允许的并会给出错误。

To fix, add 'dh_setting_context' => null, to the other entries.要修复,请将'dh_setting_context' => null,添加到其他条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM