[Doctrine Migrations] 数据库迁移组件的深刻解析三:自定义数据字段类型

自定义type

根据官方文档,新建TinyIntType类,集成Type,并重写getNamegetSqlDeclarationconvertToPHPValuegetBindingType等方法。php

TinyIntType.php完整代码:html

<?php
namespace db\types;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
/**
 * 扩展DBAL组件类型
 *
 * 迁移组件依赖的DBAL组件默认并不支持tinyint类型
 * 此类是为mysql支持tinyint类型而扩展的类
 *
 * @author tangbo<admin@tbphp.net>
 */
class TinyIntType extends Type
{
    public function getName()
    {
        return 'tinyint';
    }
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        $sql = 'TINYINT';
        if (is_numeric($fieldDeclaration['length']) && $fieldDeclaration['length'] > 1) {
            $sql .= '(' . ((int) $fieldDeclaration['length']) . ')';
        } else {
            $sql .= '(3)';
        }
        if (!empty($fieldDeclaration['unsigned'])) {
            $sql .= ' UNSIGNED';
        }
        if (!empty($fieldDeclaration['autoincrement'])) {
            $sql .= ' AUTO_INCREMENT';
        }
        return $sql;
    }
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return (null === $value) ? null : (int) $value;
    }
    public function getBindingType()
    {
        return ParameterType::INTEGER;
    }
}

其中getSqlDeclaration方法是用于生成sql语句,须要根据传入的参数处理sql拼接。mysql

注册自定义类型

Type::addType(TinyIntType::TYPENAME, 'db\types\TinyIntType');
$connection->getDatabasePlatform()->registerDoctrineTypeMapping(TinyIntType::TYPENAME, TinyIntType::TYPENAME);

这样,你在编写迁移类代码的时候就能够使用tinyint了,例如:laravel

public function up(Schema $schema): void
{
    $table = $schema->createTable('test1');
    $table->addColumn('id', 'integer')->setUnsigned(true)->setAutoincrement(true);
    $table->addColumn('status', 'tinyint')->setLength(2)->setDefault(1);
    $table->setPrimaryKey(['id']);
}

解决enum类型冲突

迁移组件不支持enum类型,也没法自定义该类型。若是集成迁移组件的时候数据库里已经存在表且有enum类型的字段,那么执行迁移命令时就会报错。git

为了解决这个问题,咱们须要把enum映射为string类型便可:github

$connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

结语

至此,咱们已经完成了迁移组件的全部迁移工做,而且已经能很好的在项目中使用它。sql

目前集成的是版本管理的方式,是迁移组件默认支持的,也是laravel框架集成的迁移方式。还有以前说到的另外一种diff方式的迁移,diff方式可以更精准的控制表结构。数据库

下一章咱们来详细研究如何集成diff方式的数据迁移组件。app

个人代码库能够查看这篇文章的详细代码,欢迎star。
相关文章
相关标签/搜索