首页 > 解决方案 > Doctrine 自定义 MariaDB 平台

问题描述

我已经基于现有的自定义方法创建了CustomDbPlatform扩展。我不能扩展,因为它是最后一堂课。MySqlPlatformMariaDb1027PlatformgetDefaultValueDeclarationSQLMariaDbPlatform

class CustomDbPlatform extends MySqlPlatform
{
    public function getJsonTypeDeclarationSQL(array $column): string
    {
        return 'LONGTEXT';
    }

    protected function getReservedKeywordsClass(): string
    {
        return MariaDb102Keywords::class;
    }

    protected function initializeDoctrineTypeMappings(): void
    {
        parent::initializeDoctrineTypeMappings();

        $this->doctrineTypeMapping['json'] = Types::JSON;
    }

    public function getDefaultValueDeclarationSQL($column): string
    {
        if (isset($column['default'], $column['type'])) {
            $default = $column['default'];
            $type = $column['type'];

            if ($type instanceof DateTimePrecisionType && stripos($default, $this->getCurrentTimestampSQL()) !== false) {
                if (isset($column['length']) && $column['length'] > 0 && $column['length'] < 255) {
                    return ' DEFAULT ' . sprintf('%s(%d)', $this->getCurrentTimestampSQL(), $column['length']);

                }

                return ' DEFAULT ' . $this->getCurrentTimestampSQL();
            }
        }

        return parent::getDefaultValueDeclarationSQL($column);
    }
}

不幸的是,在模式更新期间,Doctrine 强制使用新的表列定义基本上更新整个模式。此问题是由MySqlSchemaManager专门针对MariaDb1027Platform.

if ($this->_platform instanceof MariaDb1027Platform) {
        $columnDefault = $this->getMariaDb1027ColumnDefault($this->_platform, $tableColumn['default']);
    } else {
        $columnDefault = $tableColumn['default'];
    }

我怎样才能让 Doctrine 承认我CustomDbPlatform的身份MariaDb1027Platform?制作自定义 SchemaManager 似乎有点过头了。

使用

标签: phpmysqlsymfonydoctrinemariadb

解决方案


如果要扩展MariaDb1027Platform,唯一的方法是:

  1. 从https://github.com/doctrine/dbal/blob/3.1.x/src/Platforms/MariaDb1027Platform.php复制平台类文件,
  2. 地方在你的项目中
  3. 删除final千瓦
  4. 在从 DBAL lib 自动加载类之前加载此文件 - 您可以使用 composer autoloadfiles部分

另外,我创建了 PR https://github.com/doctrine/dbal/pull/4909,看看维护者是否想正式支持这个。


推荐阅读