"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Ignore custom indexes on Doctrine DBAL 4

Ignore custom indexes on Doctrine DBAL 4

Published on 2024-07-31
Browse:952

Ignore custom indexes on Doctrine DBAL 4

You can create database indexes using #[ORM\Index(fields: ['fieldName'] attribute on the entity file and Doctrine will do the rest to manage the index for you, but I will not talk about that.

As your project grows or requires specific requirements, you may need to use custom index types like GIST, GIN, BRIN, etc (https://www.postgresql.org/docs/current/indexes-types.html). Doctrine does not support creating database-vendor-specific index types out-of-the-box (ref).

To tackle this issue you can either execute the CREATE INDEX DDL directly to the database or write the DDL on doctrine-migrations file. The latter option gives you the benefit of easier to deploy or rollback the changes.

No matter which method you use to create the custom index, Doctrine will always mark those custom indexes as unmapped indexes hence if you execute doctrine:schema:validate you will get an error that state if your database is not in sync, the same while executing doctrine:schema:update --dump-sql or doctrine:migrations:diff it will show you DROP INDEX ... statement to remove the custom indexes.

Solution

I'm using these package versions. (I believe the solution will work on the same major version of the packages):

  • doctrine/dbal 4.0.4
  • doctrine/doctrine-bundle 2.12.0

I found several tutorials to handle this but it is not satisfying:

  • https://www.liip.ch/en/blog/doctrine-and-generated-columns This was not working anymore as Doctrine DBAL removes the event manager on DBAL 4. (ref)
  • https://medium.com/yousign-engineering-product/ignore-custom-indexes-on-doctrine-dbal-b5131dd22071 This was showing a deprecation message The "platform_service" configuration key is deprecated since doctrine-bundle 2.9. DBAL 4 will not support setting a custom platform via connection params anymore. (ref)

I found a GitHub issue about platform_service config replacement here https://github.com/doctrine/DoctrineBundle/issues/1656.

Reading these 2 pages gave me all the pieces of information on how to use a custom DBAL platform through Doctrine middleware:

  • https://github.com/doctrine/dbal/pull/5699
  • https://symfony.com/bundles/DoctrineBundle/current/middlewares.html

Finally, after digging into the source code, I found the solution. You need to create 4 files, 2 files are about Doctrine middleware and 2 other files are about Doctrine DBAL platform and schema.

Modify the namespace and class name as your needs.



















 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;
    }
}
Release Statement This article is reproduced at: https://dev.to/indragunawan/ignore-custom-indexes-on-doctrine-dbal-4-387j?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3