Skip to content

Commit

Permalink
Merge pull request #29 from RamyHakam/28-unable-to-use-dql-string_fun…
Browse files Browse the repository at this point in the history
…ctions

Add DQL functions to Tenant Entity Manager
  • Loading branch information
RamyHakam authored Nov 30, 2023
2 parents a6fe382 + 360b9ab commit 06cc4be
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions src/DependencyInjection/HakamMultiTenancyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
[
Expand Down Expand Up @@ -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'];
}
}
}
8 changes: 8 additions & 0 deletions tests/Functional/ServiceWiringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 06cc4be

Please sign in to comment.