Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Doc][Advanced][Doctrine][Schema] Allow set callable class in doctrine.dbal.schema_filter configuration parameter #20996

Open
@ksn135

Description

@ksn135

Hi there!

Hoping it will help someone and be useful to someone else

You are free to use it as you see fit.

Problem

Sometimes when you have to rewrite an old monolith you face such a problem when there are a lot of tables in the database (more than seven hundred tables) that are not related to the implemented functionality, but they cannot be touched.

There is a convenient implemented functionality in Doctrine that allows you to filter tables by different criteria, setting your own anonymous function for filtering, using a methodsetSchemaAssetsFilter.

$config->setSchemaAssetsFilter(function (string|AbstractAsset$assetName):bool {if ($assetNameinstanceof AbstractAsset) {$assetName =$assetName->getName();          }return !str_starts_with($assetName,'audit_');      });

But the current Symfony 7.3 configuration setting does not allow to add its own anonymous function (Callable) to thedoctrine.dbal.schema_filter parameter. It's only a text regular expression allowed.

The proposed solution, allows you to specify your class name and automatically call its __invoke(AbstractAsset|string $assetName): bool method with any logic required by the developer.

<?php// config/packages/doctrine.phpuseApp\Common\Doctrine\SchemaFilter\DoctrineIgnoreTables;useApp\Common\Doctrine\Types\JsonArrayType;useSymfony\Bridge\Doctrine\Types\DatePointType;useSymfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;returnstaticfunction (ContainerConfigurator$containerConfigurator):void {$containerConfigurator->extension('doctrine', ['dbal' => ['url' =>'%env(resolve:DATABASE_URL)%','profiling_collect_backtrace' =>'%kernel.debug%','use_savepoints' =>true,'types' => [                DatePointType::NAME => DatePointType::class,                JsonArrayType::NAME => JsonArrayType::class,            ],// Just imagine listing over 700 tables here!!!// 'schema_filter' => '/^prefix_/', // <== the OLD RegExp way// YOU don't need it anymore.        ],'orm' => ['auto_generate_proxy_classes' =>true,// ....        ]);    }};

Solution

<?php// src/Common/di.phpdeclare(strict_types=1);/* * This file is part of BAZE informational system package. * * (c) Serg N. Kalachev <serg@kalachev.ru> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */namespaceApp\Common;useApp\Common\Doctrine\DBAL\PrefixSchemaAssetFilter;// <== see belowuseSymfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;returnstaticfunction (ContainerConfigurator$containerConfigurator):void {$containerConfigurator        ->services()        ->defaults()        ->autowire()        ->autoconfigure()        ->load(__NAMESPACE__.'\\','.')        ->exclude(['./{di.php,routing.php}',// './**/{Command,Filter,Message}.php',        ])    ;$containerConfigurator        ->services()        ->set(PrefixSchemaAssetFilter::class)         ->tag('doctrine.dbal.schema_filter', ['connection' =>'default'])        ->arg(0, ['1c_contractor','approver','archive','audit','author','bank','bkr','boss','box','budget','cabinet',/** ... skipping over hundred lines here!!! */'warehouse','waybill','workflow',        ])    ;$containerConfigurator->extension('doctrine', ['orm' => ['mappings' => [__NAMESPACE__ => ['is_bundle' =>false,'type' =>'xml','dir' =>__DIR__.'/Doctrine/ORM/Mapping','prefix' =>__NAMESPACE__.'\Doctrine\Entity','alias' =>basename(__DIR__),                ],            ],        ],    ]);$containerConfigurator->extension('twig', ['paths' => [__DIR__.'/templates' =>basename(__DIR__),        ],    ]);};
<?php// src/Common/Doctrine/DBAL/PrefixSchemaAssetFilter.phpdeclare(strict_types=1);/* * This file is part of BAZE informational system package. * * (c) Serg N. Kalachev <serg@kalachev.ru> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */namespaceApp\Common\Doctrine\DBAL;useDoctrine\DBAL\Schema\AbstractAsset;usefunctionstr_starts_with;class PrefixSchemaAssetFilter{/** @param string[] $prefixes */publicfunction__construct(privatereadonlyarray$prefixes,    ) {}/** @param AbstractAsset|string $assetName */publicfunction__invoke($assetName):bool    {if ($assetNameinstanceof AbstractAsset) {$assetName =$assetName->getName();        }foreach ($this->prefixesas$prefix) {if (str_starts_with($assetName,$prefix)) {returnfalse;            }        }returntrue;    }}

That's all folks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp