在 Yii 2.0 中执行数据库迁移:添加 MySQL 字段的 MEDIUMTEXT 类型
1、在 Yii 2.0 中执行数据库迁移:MySQL 的 MEDIUMTEXT 类型。四个 TEXT 类型是 TINYTEXT、TEXT、MEDIUMTEXT、LONGTEXT。仅支持:TEXT
// The following are the supported abstract column data types. const TYPE_PK = 'pk'; const TYPE_UPK = 'upk'; const TYPE_BIGPK = 'bigpk'; const TYPE_UBIGPK = 'ubigpk'; const TYPE_CHAR = 'char'; const TYPE_STRING = 'string'; const TYPE_TEXT = 'text'; const TYPE_TINYINT = 'tinyint'; const TYPE_SMALLINT = 'smallint'; const TYPE_INTEGER = 'integer'; const TYPE_BIGINT = 'bigint'; const TYPE_FLOAT = 'float'; const TYPE_DOUBLE = 'double'; const TYPE_DECIMAL = 'decimal'; const TYPE_DATETIME = 'datetime'; const TYPE_TIMESTAMP = 'timestamp'; const TYPE_TIME = 'time'; const TYPE_DATE = 'date'; const TYPE_BINARY = 'binary'; const TYPE_BOOLEAN = 'boolean'; const TYPE_MONEY = 'money'; const TYPE_JSON = 'json';
2、参考文件:\vendor\yiisoft\yii2\db\SchemaBuilderTrait.php,复制至 \console\db\mysql\SchemaBuilderTrait.php,参考 TEXT 类型的实现,添加对 TINYTEXT、MEDIUMTEXT、LONGTEXT 的支持
<?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace console\db\mysql; use yii\base\NotSupportedException; use yii\db\Connection; use yii\db\ColumnSchemaBuilder; /** * SchemaBuilderTrait contains shortcut methods to create instances of [[ColumnSchemaBuilder]]. * * These can be used in database migrations to define database schema types using a PHP interface. * This is useful to define a schema in a DBMS independent way so that the application may run on * different DBMS the same way. * * For example you may use the following code inside your migration files: * * ```php * $this->createTable('example_table', [ * 'id' => $this->primaryKey(), * 'name' => $this->string(64)->notNull(), * 'type' => $this->integer()->notNull()->defaultValue(10), * 'description' => $this->text(), * 'rule_name' => $this->string(64), * 'data' => $this->text(), * 'created_at' => $this->datetime()->notNull(), * 'updated_at' => $this->datetime(), * ]); * ``` * * @author Qiang Wang <shuijingwanwq@163.com> * @since 1.0 */ trait SchemaBuilderTrait { /** * @return Connection the database connection to be used for schema building. */ abstract protected function getDb(); /** * Creates a tinytext column. * @return ColumnSchemaBuilder the column instance which can be further customized. * @throws NotSupportedException if there is no support for the current driver type * @since 2.0.6 */ public function tinyText() { return $this->getDb()->getSchema()->createColumnSchemaBuilder('tinytext'); } /** * Creates a mediumtext column. * @return ColumnSchemaBuilder the column instance which can be further customized. * @throws NotSupportedException if there is no support for the current driver type * @since 2.0.6 */ public function mediumText() { return $this->getDb()->getSchema()->createColumnSchemaBuilder('mediumtext'); } /** * Creates a longtext column. * @return ColumnSchemaBuilder the column instance which can be further customized. * @throws NotSupportedException if there is no support for the current driver type * @since 2.0.6 */ public function longText() { return $this->getDb()->getSchema()->createColumnSchemaBuilder('longtext'); } }
3、在数据库迁移文件中,引用新建的 Trait,以添加字段类型:MEDIUMTEXT
<?php use yii\base\NotSupportedException; use yii\db\Migration; use console\db\mysql\SchemaBuilderTrait; /** * Class m200102_030708_pre_pub_log */ class m200102_030708_pre_pub_log extends Migration { use SchemaBuilderTrait; /** * {@inheritdoc} * @throws NotSupportedException if there is no support for the current driver type */ public function safeUp() { $tableOptions = null; if ($this->db->driverName === 'mysql') { $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB COMMENT="预发布日志"'; } $this->createTable('{{%pre_pub_log}}', [ 'id' => $this->primaryKey(), 'pub_log_message' => $this->mediumText()->notNull()->comment('发布日志说明'), ], $tableOptions); } /** * {@inheritdoc} */ public function safeDown() { echo "m200102_030708_pre_pub_log cannot be reverted.\n"; return false; } /* // Use up()/down() to run migration code without a transaction. public function up() { } public function down() { echo "m200102_030708_pre_pub_log cannot be reverted.\n"; return false; } */ }
4、执行数据库迁移,生成表:预发布日志,查看字段:pub_log_message,其类型为:mediumtext,符合预期,如图1
PS E:\wwwroot\channel-pub-api-feature-success-error-tasks> ./yii migrate Yii Migration Tool (based on Yii v2.0.30) Total 1 new migration to be applied: m200102_030708_pre_pub_log Apply the above migration? (yes|no) [no]:yes *** applying m200102_030708_pre_pub_log > create table {{%pre_pub_log}} ... done (time: 0.280s) *** applied m200102_030708_pre_pub_log (time: 0.394s) 1 migration was applied. Migrated up successfully.
近期评论