How to set encoding to a specific column during Yii2 migration

I have a migration to Yii2 where I am trying to create a table. I set the encoding for the table, but I don't know how to set the encoding for a specific column.

For instance:

$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 the above code, I want to set the encoding "utf8-unicode-ci" to column_1. How to do it?

+5
source share
2 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 this just an example? Because you do not need to set the encoding for one column if it is the same as for the whole table.

+4
source

For MySQL, you can use the following trick:

 $schema = $this->getDb()->getSchema(); $columnBase = $schema->createColumnSchemaBuilder($schema::TYPE_STRING, 255); $columnExtension = $schema->createColumnSchemaBuilder('CHARACTER SET utf8 COLLATE utf8_bin'); $columnExtension->notNull(); $this->createTable('{{%table1}}', [ 'column1' => $columnBase . ' ' . $columnExtension, ]); 
0
source

All Articles