composer require cekurte/doctrinebundle
config/bundles.php:
return [
// ...
Cekurte\DoctrineBundle\CekurteDoctrineBundle::class => ['all' => true],
];
config/packages/cekurte_doctrine.yaml:
cekurte_doctrine:
default_connection: default
connections:
default: "%env(DATABASE_URL)%"
secondary: "mysql://user:pass@localhost:3306/secondary_db"
CekurteDoctrineBundle\RuntimeConnectionManager service and switch connections dynamically:
use Cekurte\DoctrineBundle\RuntimeConnectionManager;
public function __construct(RuntimeConnectionManager $connectionManager) {
$this->connectionManager = $connectionManager;
}
public function switchToSecondary() {
$this->connectionManager->setConnection('secondary');
// Now all DBAL operations use the secondary connection
}
RuntimeConnectionManager to services needing dynamic DB switching.
class MultiDBService {
public function __construct(RuntimeConnectionManager $manager) {
$this->manager = $manager;
}
public function readFromPrimary() {
$this->manager->setConnection('primary');
// Query logic here
}
}
public function handle(Request $request, Closure $next) {
$tenantId = $request->attributes->get('tenant_id');
$this->manager->setConnection("tenant_{$tenantId}");
return $next($request);
}
class TenantRepository extends ServiceEntityRepository {
public function __construct(RuntimeConnectionManager $manager) {
$this->manager = $manager;
}
public function findByTenant($tenantId) {
$this->manager->setConnection("tenant_{$tenantId}");
return parent::findBy([]);
}
}
Connection service directly with the bundle’s manager:
$connection = $this->connectionManager->getConnection();
$query = $connection->createQueryBuilder();
$this->connectionManager->setConnection('primary');
$connection = $this->connectionManager->getConnection();
$connection->beginTransaction();
try {
// Operations...
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
throw $e;
}
$dispatcher->addListener(
CekurteDoctrineEvents::CONNECTION_SWITCH,
function (ConnectionSwitchEvent $event) {
// Log or validate the new connection
}
);
try {
$this->connectionManager->setConnection('secondary');
// Operations...
} finally {
$this->connectionManager->setConnection('default');
}
dbal bundle. Ensure doctrine/dbal is installed and configured separately for each connection.public function debugConnection() {
echo "Current connection: " . $this->connectionManager->getConnectionName();
}
doctrine:
dbal:
logging: true
profiling: true
tenant_{id}):
$this->connectionManager->setConnection("tenant_" . $tenantId);
try {
$this->connectionManager->setConnection('primary');
} catch (\Exception $e) {
$this->connectionManager->setConnection('fallback');
}
# config/services.yaml
Cekurte\DoctrineBundle\RuntimeConnectionManager:
arguments:
$container: '@service_container'
%env() syntax for connection URLs to avoid hardcoding:
connections:
default: "%env(DATABASE_URL)%"
secondary: "%env(SECONDARY_DATABASE_URL)%"
preFlush). Handle connection-specific events manually.How can I help you explore Laravel packages today?