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

WBOY
发布: 2024-07-28 08:01:31
转载
261人浏览过

忽略 doctrine dbal 4 上的自定义索引

你可以使用实体文件上的 #[ormindex(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

我找到了几个教程来处理这个问题,但并不令人满意:

纳米搜索
纳米搜索

纳米搜索:360推出的新一代AI搜索引擎

纳米搜索 30
查看详情 纳米搜索
  • 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 平台和架构的。

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

<?php

declare(strict_types=1);

namespace app\doctrine\dbal\middleware;

use doctrine\dbal\driver;
use doctrine\dbal\driver\middleware;

final class postgresqlplatformmiddleware implements middleware
{
    #[\override]
    public function wrap(driver $driver): driver
    {
        return new postgresqlplatformdriver($driver);
    }
}
登录后复制
<?php

declare(strict_types=1);

namespace app\doctrine\dbal\middleware;

use app\doctrine\dbal\platforms\postgresqlplatform;
use doctrine\dbal\driver\middleware\abstractdrivermiddleware;
use doctrine\dbal\platforms\abstractplatform;
use doctrine\dbal\serverversionprovider;

final class postgresqlplatformdriver extends abstractdrivermiddleware
{
    #[\override]
    public function getdatabaseplatform(serverversionprovider $versionprovider): abstractplatform
    {
        return new postgresqlplatform();
    }
}

登录后复制
<?php

declare(strict_types=1);

namespace app\doctrine\dbal\platforms;

use app\doctrine\dbal\schema\postgresqlschemamanager;
use doctrine\dbal\connection;
use doctrine\dbal\platforms\postgresqlplatform as basepostgresqlplatform;
use doctrine\dbal\schema\postgresqlschemamanager as basepostgresqlschemamanager;
use doctrine\dbal\schema\schemamanagerfactory;

final class postgresqlplatform extends basepostgresqlplatform implements schemamanagerfactory
{
    #[\override]
    public function createschemamanager(connection $connection): basepostgresqlschemamanager
    {
        return new postgresqlschemamanager($connection, $this);
    }
}
登录后复制
<?php

declare(strict_types=1);

namespace App\Doctrine\DBAL\Schema;

use Doctrine\DBAL\Schema\PostgreSQLSchemaManager as BasePostgreSQLSchemaManager;

final class PostgreSQLSchemaManager extends BasePostgreSQLSchemaManager
{
    private const array IGNORED_INDEXES = [
        'index_name_1' => 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;
    }
}
登录后复制

以上就是忽略 Doctrine DBAL 4 上的自定义索引的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号