Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DQL functions to Tenant Entity Manager #29

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading