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. Why does dh_setting_context' => 'user1'
generator an error and how to fix it, please?
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' => '[email protected]',
'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
CodePudding user response:
Not all of the entries in $data
have the same keys.
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
.
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.
To fix, add 'dh_setting_context' => null,
to the other entries.