如何在Yii2的迁移中将字符集设置为特定列

13

我在 Yii2 中有一个迁移,想要创建一张表。我为表设置了字符集,但是我不知道如何为特定列设置字符集。

例如:

$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');

在上述代码中,我想为column_1设置字符集"utf8-unicode-ci"。 怎么做呢?

2个回答

16

使用 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');

这只是一个例子吗?因为当单个列与整个表格相同时,您无需为单个列设置字符集。


0

对于MySQL,您可以使用以下技巧:

$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,
]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接