Reputation: 53
I am having issue with Multi Tenancy in PHP API PLATFORM when I am trying something like this with Wrapper Class:
doctrine:
dbal:
connections:
default:
wrapper_class: App\Doctrine\TenantConnection
driver: pdo_mysql
host: '%env(resolve:DATABASE_HOST)%'
port: 3306
user: '%env(resolve:DATABASE_USER)%'
password: '%env(resolve:DATABASE_PASSWORD)%'
charset: UTF8MB4
class TenantConnection extends Connection
{
public function __construct(array $params, $driver, $config)
{
$tenantId = $_SERVER['HTTP_TENANT_ID'] ?? null;
$dbName = // get by $tenantId from json (simply)
$params['dbname'] = $dbName; // dynamic change of DB Name
try {
parent::__construct($params, $driver);
} catch(ConnectionException $e) {
}
}
}
When I running this locally it all works fine. But when I build dockefile to GKE its failing because the $_SERVER['HTTP_TENANT_ID']
is not defined (at some experiment, when I try to call controller which was returning $_SERVER['HTTP_TENANT_ID']
it was all fine and present).
Is there problem with Caddy/Franken on production? I thing its loading all container into cache so all http requests will be faster. But how can I modify my Multi Tenancy experiment to make that work? Or is it bad practice? I would like to avoid to have multiple backend where only change will be dbName (from env). Thanks for any help.
Upvotes: 2
Views: 116
Reputation: 1
params should be private. You have to initalize new connection
public function changeDatabase(Tenant $tenant): bool
{
$params = $this->getParams();
if ($params['dbname'] !== $tenant->getDatabaseParam()->getName()) {
if ($this->isConnected()) {
$this->close();
}
$params['url'] = "postgresql://" . $params['user'] . ":" . $params['password'] . "@" . $params['host'] . ":" . $params['port'] . "/" . $tenant->getDatabaseParam()->getName(
);
$params['dbname'] = $tenant->getDatabaseParam()->getName();
parent::__construct(
$params,
$this->_driver,
$this->_config,
$this->_eventManager
);
return true;
}
return false;
}
Upvotes: -1