”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 忽略 Doctrine DBAL 4 上的自定义索引

忽略 Doctrine DBAL 4 上的自定义索引

发布于2024-07-31
浏览:914

Ignore custom indexes on Doctrine DBAL 4

您可以使用实体文件上的 #[ORM\Index(fields: ['fieldName'] 属性创建数据库索引,Doctrine 将完成剩下的工作来为您管理索引,但是我不会谈论这个。

随着您的项目增长或需要特定要求,您可能需要使用自定义索引类型,如 GIST、GIN、BRIN 等 (https://www.postgresql.org/docs/current/indexes-types.html)。 Doctrine 不支持创建开箱即用的特定于数据库供应商的索引类型(参考)。

要解决此问题,您可以直接对数据库执行 CREATE INDEX DDL,或者将 DDL 写入到doctrine-migrations 文件中。后一个选项使您可以更轻松地部署或回滚更改。

无论您使用哪种方法创建自定义索引,Doctrine 都会将这些自定义索引始终标记为未映射索引,因此如果您执行 Doctrine:schema:validate 您将收到一个错误,指出如果您的数据库不同步,同样,在执行doctrine:schema:update --dump-sql 或doctrine:migrations:diff 时,它会显示 DROP INDEX ... 语句以删除自定义索引。

解决方案

我正在使用这些软件包版本。 (我相信该解决方案将适用于相同主要版本的软件包):

    学说/dbal 4.0.4
  • 学说/学说包 2.12.0
我找到了几个教程来处理这个问题,但并不令人满意:

    https://www.liip.ch/en/blog/doctrine-and- generated-columns 这不再起作用,因为 Doctrine DBAL 删除了 DBAL 4 上的事件管理器。(参考)
  • https://medium.com/yousign-engineering-product/ignore-custom-indexes-on-doctrine-dbal-b5131dd22071 这显示了一条弃用消息“platform_service”配置密钥自doctrine-bundle 2.9以来已弃用。 DBAL 4 将不再支持通过连接参数设置自定义平台。 (参考)
我在这里发现了一个关于 platform_service 配置替换的 GitHub 问题 https://github.com/doctrine/DoctrineBundle/issues/1656。

阅读这两页为我提供了有关如何通过 Doctrine 中间件使用自定义 DBAL 平台的所有信息:

    https://github.com/doctrine/dbal/pull/5699
  • https://symfony.com/bundles/DoctrineBundle/current/middlewares.html
最后,在深入研究源代码后,我找到了解决方案。您需要创建4个文件,其中2个文件是关于Doctrine中间件的,另外2个文件是关于Doctrine DBAL平台和模式的。

根据需要修改命名空间和类名。

真, 'index_name_2' => 真, ]; #[\覆盖] 受保护函数 _getPortableTableIndexesList(数组 $tableIndexes, 字符串 $tableName): 数组 { $indexes = 父级::_getPortableTableIndexesList($tableIndexes, $tableName); foreach (array_keys($indexes) as $indexName) { if (isset(self::IGNORED_INDEXES[$indexName])) { 取消设置($indexes[$indexName]); } } 返回$索引; } }
 true,
        'index_name_2' => true,
    ];

    #[\Override]
    protected function _getPortableTableIndexesList(array $tableIndexes, string $tableName): array
    {
        $indexes = parent::_getPortableTableIndexesList($tableIndexes, $tableName);

        foreach (array_keys($indexes) as $indexName) {
            if (isset(self::IGNORED_INDEXES[$indexName])) {
                unset($indexes[$indexName]);
            }
        }

        return $indexes;
    }
}
版本声明 本文转载于:https://dev.to/indragunawan/ignore-custom-indexes-on-doctrine-dbal-4-387j?1如有侵犯,请联系[email protected]删除
最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3