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

Clear Doctrine cache after connection switch #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion src/EventListener/DbSwitchEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hakam\MultiTenancyBundle\EventListener;

use Hakam\MultiTenancyBundle\Doctrine\ORM\TenantEntityManager;
use Hakam\MultiTenancyBundle\Event\SwitchDbEvent;
use Hakam\MultiTenancyBundle\Services\DbConfigService;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -16,7 +17,8 @@ class DbSwitchEventListener implements EventSubscriberInterface
public function __construct(
private ContainerInterface $container,
private DbConfigService $dbConfigService,
private string $databaseURL
private TenantEntityManager $tenantEntityManager,
private string $databaseURL,
)
{
}
Expand All @@ -33,14 +35,19 @@ public function onHakamMultiTenancyBundleEventSwitchDbEvent(SwitchDbEvent $switc
{
$dbConfig = $this->dbConfigService->findDbConfig($switchDbEvent->getDbIndex());
$tenantConnection = $this->container->get('doctrine')->getConnection('tenant');

$params = [
'dbname' => $dbConfig->getDbName(),
'user' => $dbConfig->getDbUsername() ?? $this->parseDatabaseUrl($this->databaseURL)['user'],
'password' => $dbConfig->getDbPassword() ?? $this->parseDatabaseUrl($this->databaseURL)['password'],
'host' => $dbConfig->getDbHost() ?? $this->parseDatabaseUrl($this->databaseURL)['host'],
'port' => $dbConfig->getDbPort() ?? $this->parseDatabaseUrl($this->databaseURL)['port'],
];

$tenantConnection->switchConnection($params);

//clear the entity manager to avoid Doctrine cache issues
$this->tenantEntityManager->clear();
}

private function parseDatabaseUrl(string $databaseURL): array
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<tag name="kernel.event_listener" event="Hakam\MultiTenancyBundle\Event\SwitchDbEvent"/>
<argument type="service" id="service_container"/>
<argument type="service" id="hakam_db_config.service"/>
<argument type="service" id="tenant_entity_manager"/>
<argument type="string">%env(DATABASE_URL)%</argument>
</service>

Expand Down Expand Up @@ -58,4 +59,4 @@
</service>
<service id="Hakam\MultiTenancyBundle\Doctrine\ORM\TenantEntityManager" alias="tenant_entity_manager"/>
</services>
</container>
</container>