Doctrine 自定义 MariaDB 平台

3

我创建了一个CustomDbPlatform,扩展自MySqlPlatform,并基于现有的MariaDb1027Platform,添加了定制的getDefaultValueDeclarationSQL方法。由于MariaDbPlatform是一个终态类,所以我无法对其进行扩展。

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似乎过于复杂。 使用
  • Symfony 4.4
  • Doctrine 2
  • MariaDB 10.3

1
扩展 MySqlSchemaManager 并仅覆盖 _getPortableTableColumnDefinition 方法是否可行?也许这样做会更简单,但似乎其他任何方法都需要对 Doctrine 库进行更改。 - domagoj
1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接