没有不值得去解决的问题,也没有不值得去学习的技术!

在 Yii 2.0 中执行数据库迁移:添加 MySQL 字段的 MEDIUMTEXT 类型

执行数据库迁移,生成表:预发布日志,查看字段:pub_log_message,其类型为: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

执行数据库迁移,生成表:预发布日志,查看字段: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.


PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理