Yii2 - 使用LONGBLOB字段创建迁移

时间:2016-09-27 09:47:07

标签: mysql yii2

我想在其中创建一个包含 LONGBLOB 字段的表格。

'image' => $this->binary(),

在表格中生成 BLOB 字段。

除了使用特定字段的原始 SQL 语法之外,还有其他方法可以生成 LONGBLOB 字段。

以下是我创建表格的完整代码。

    $this->createTable('edition_images', [
        'image_id' => $this->bigPrimaryKey()->unsigned(),

        'embed_url' => $this->string()->notNull(),

        'image_type' => $this->string(),
        'image_md5' => $this->string(),
        //'image' => $this->binary(),
        '`image` longblob NULL',

        'title_en' => $this->string(),
        'title_bg' => $this->string(),
        'title_ro' => $this->string(),

        'order' => $this->bigInteger(20)->unsigned()->null(),

        'edition_id' => $this->bigInteger(20)->unsigned()->notNull(),

        'created_by' => $this->bigInteger(20)->unsigned()->notNull(),
        'created_at' => $this->timestamp()->notNull()->defaultExpression('CURRENT_TIMESTAMP'),
        'updated_by' => $this->bigInteger(20)->unsigned()->null(),
        'updated_at' => $this->timestamp()->null()->defaultValue(null)->append('ON UPDATE CURRENT_TIMESTAMP'),
        'deleted_by' => $this->bigInteger(20)->unsigned()->null(),
        'deleted_at' => $this->timestamp()->null(),
        'deleted' => $this->integer(1),
    ]);

1 个答案:

答案 0 :(得分:1)

您可以将确切的列类型作为文本传递:

'image' => 'LONGBLOB'

您应该能够在binary方法中将长度指定为参数。由于longblob是4GB,你必须以字节为单位指定:

'image' => $this->binary(4294967295),

但是,$length被忽略了。代码

$this->db->createCommand()->createTable("test_blob", [
    "id"    => $this->integer(),
    "datum" => $this->binary(429496729),
    "txt"   => $this->string()
])->getSql();

返回以下SQL:

CREATE TABLE `test_blob` (
    `id` int(11),
    `datum` blob,
    `txt` varchar(255)
);

我添加了issue on Github