Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 438
Ratbek Avatar asked Aug 18 '16 06:08

Ratbek


People also ask

How do I run a specific migration in yii2?

To run specific migration, you can mark(skip) migrations upto just before one you want run. You can mark migration by using one of following command: Using timestamp to specify the migration yii migrate/mark 150101_185401. Using a string that can be parsed by strtotime() yii migrate/mark "2015-01-01 18:54:01"

How can I add foreign key in Yii?

Here is how I try to add FK: $this->addForeignKey('FK_user_profile', 'tbl_profile', 'user_id', 'tbl_user', 'id', 'CASCADE', 'CASCADE');


1 Answers

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.

like image 144
Bizley Avatar answered Sep 18 '22 23:09

Bizley