Search code examples
character-encodingyii2migration

How to set charset to particular column in migration of Yii2


I have a migration in Yii2, where I try create a table. I set charset for table, but I don't know how to set charset for particular column.

For example:

$this->createTable('some_table', [
            'column_1' => $this->string(64)->notNull(),
            'column_2' => $this->integer()->notNull(),
            'column_3' => $this->integer(),
        ], 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB');

In above code I want to set charset "utf8-unicode-ci" for column_1. How to do that?


Solution

  • Use append().

    $this->createTable('some_table', [
        'column_1' => $this->string(64)->notNull()->append('CHARACTER SET utf8 COLLATE utf8_unicode_ci'),
        'column_2' => $this->integer()->notNull(),
        'column_3' => $this->integer(),
    ], 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB');
    

    Is it just an example? Because you don't have to set charset for a single column when it's the same as for the whole table.