From 263b1ea5b1ac96400c2cf5039cc2acc6b787f778 Mon Sep 17 00:00:00 2001 From: Ramy Hakam Date: Thu, 30 Nov 2023 17:31:41 +0100 Subject: [PATCH 1/2] Add DQL functions to Tenant Entity Manager --- src/DependencyInjection/Configuration.php | 16 ++++++++++++++++ .../HakamMultiTenancyExtension.php | 8 ++++++++ tests/Functional/ServiceWiringTest.php | 8 ++++++++ 3 files changed, 32 insertions(+) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index f665928..1fcd9c5 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -52,6 +52,22 @@ public function getConfigTreeBuilder(): TreeBuilder ->addDefaultsIfNotSet() ->children() ->variableNode('tenant_naming_strategy')->defaultValue('doctrine.orm.naming_strategy.default')->end() + ->arrayNode("dql")->canBeUnset()->info('tenant entity manager dql configuration') + ->children() + ->arrayNode('string_functions') + ->useAttributeAsKey('name') + ->prototype('scalar')->end() + ->end() + ->arrayNode('numeric_functions') + ->useAttributeAsKey('name') + ->prototype('scalar')->end() + ->end() + ->arrayNode('datetime_functions') + ->useAttributeAsKey('name') + ->prototype('scalar')->end() + ->end() + ->end() + ->end() ->arrayNode('mapping') ->info('tenant Entity Manager mapping configuration, Its recommended to have a different mapping config than your main entity config') ->ignoreExtraKeys() diff --git a/src/DependencyInjection/HakamMultiTenancyExtension.php b/src/DependencyInjection/HakamMultiTenancyExtension.php index 881ca4f..36e8063 100644 --- a/src/DependencyInjection/HakamMultiTenancyExtension.php +++ b/src/DependencyInjection/HakamMultiTenancyExtension.php @@ -77,6 +77,7 @@ public function prepend(ContainerBuilder $container) ], ]; + $this->injectTenantDqlFunctions($tenantEntityManagerConfig, $dbSwitcherConfig); $this->checkDir($container->getParameter('kernel.project_dir'), $dbSwitcherConfig['tenant_migration']['tenant_migration_path']); $tenantDoctrineMigrationPath = [ @@ -107,4 +108,11 @@ private function checkDir(string $projectDir, string $dir): void $fileSystem->mkdir($dir); } } + + private function injectTenantDqlFunctions(array &$tenantEntityManagerConfig, array $dbSwitchConfig): void + { + if (isset($dbSwitchConfig['tenant_entity_manager']['dql'])) { + $tenantEntityManagerConfig['entity_managers']['tenant']['dql'] = $dbSwitchConfig['tenant_entity_manager']['dql']; + } + } } diff --git a/tests/Functional/ServiceWiringTest.php b/tests/Functional/ServiceWiringTest.php index 04d8d11..aeedd9a 100644 --- a/tests/Functional/ServiceWiringTest.php +++ b/tests/Functional/ServiceWiringTest.php @@ -37,6 +37,14 @@ protected function setUp(): void ], 'tenant_entity_manager' => [ + 'tenant_naming_strategy' => 'doctrine.orm.naming_strategy.default', + 'dql' => + [ + 'string_functions' => + [ + 'FIELD' => 'Tenant\Functions\FieldFunction' + ] + ], 'mapping' => [ 'type' => 'annotation', From 360b9ab9eaa101acd5ac7c60003fedf680bcb716 Mon Sep 17 00:00:00 2001 From: Ramy Hakam Date: Thu, 30 Nov 2023 17:35:24 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 3de79fd..644e2c8 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,14 @@ hakam_multi_tenancy: tenant_migration_path: migrations/Tenant tenant_entity_manager: # tenant entity manger configuration , which is used to manage tenant entities tenant_naming_strategy: # tenant entity manager naming strategy + dql: # tenant entity manager dql configuration + string_functions: + test_string: App\DQL\StringFunction + second_string: App\DQL\SecondStringFunction + numeric_functions: + test_numeric: App\DQL\NumericFunction + datetime_functions: + test_datetime: App\DQL\DatetimeFunction mapping: type: attribute # mapping type default annotation dir: '%kernel.project_dir%/src/Entity/Tenant' # directory of tenant entities, it could be different from main directory