Welcome Guest, Not a member yet? Register   Sign In
Database Migrations - The charset and collation properties.
#1

Hello everyone!

Is it possible to set in CI4: "charset" and "collation" for the table, and more specifically for the columns?

For example, I have a table:
Code:
CREATE TABLE `user_test` (
/// Other columns...
  `mini_info` varchar(45) COLLATE utf8mb4_bin DEFAULT NULL,
  `user_description` text COLLATE utf8mb4_general_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

The migration file looks like the following (immediately adds what I tried to add in the array to get the appropriate collation and charset):
PHP Code:
<?php
namespace App\Database\Migrations;

use 
CodeIgniter\Database\Migration;

class 
CreateUserTest extends Migration
{
    public function up()
    {
        $this->forge->addField([
            // Other columns...
            'mini_info' => [
                'type' => 'VARCHAR',
                'constraint' => 45,
                'null' => true,
 
               'charset' => 'utf8mb4',
 
               'collation' => 'utf8mb4_bin'
            ],
            'user_description' => [
                'type' => 'TEXT',
                'null' => true,
 
               'charset' => 'utf8mb4',
 
               'collation' => 'utf8mb4_general_ci'
            ]
        ]);

        $this->forge->addKey('id'true);
        $this->forge->createTable('user_test');
    }

    public function down()
    {
        $this->forge->dropTable('user_test');
    }

Unfortunately, this does not work, I could not find any information about it in the documentation or on the Internet.

PS:
I would like to inform you that my post is not about setting "charset" and "collation" in files: "app/Config/Database.php" or .env
Pls help or advice on how to approach this.
Reply
#2

See https://codeigniter4.github.io/CodeIgnit...-as-fields
Reply
#3

kenjis thank you very much for your help and your contribution to Codeigniter development!

I will leave the code who, thanks to the reply of kenjis, I was able to write and answer my question:
PHP Code:
<?php
namespace App\Database\Migrations;

use 
CodeIgniter\Database\Migration;

class 
CreateSysUrlTable extends Migration
{
    public function up()
    {
        // Other columns...
        $this->forge->addField("mini_info varchar(45) COLLATE utf8mb4_bin DEFAULT NULL");
        $this->forge->addField("user_description text COLLATE utf8mb4_general_ci");
        $this->forge->addUniqueKey('id');
        $attributes = [
            'ENGINE' => 'InnoDB',
            'CHARSET' => 'utf8mb4',
            'COLLATE' => 'utf8mb4_general_ci'

        ];
        $this->forge->createTable('user_test'false$attributes);
    }

    public function down()
    {
        $this->forge->dropTable('user_test');
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB