To configure multiple databases in Symfony, you can follow these steps:
config/packages/doctrine.yaml
). You can define multiple database connections under the doctrine.dbal.connections
key.doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
secondary:
url: '%env(resolve:SECONDARY_DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
config/packages/doctrine.yaml
). You can define multiple entity managers under the doctrine.orm.entity_managers
key.doctrine:
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
secondary:
connection: secondary
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/SecondaryEntity'
prefix: 'App\Entity'
doctrine
service in your controllers or services.$entityManager = $this->get('doctrine')->getManager('secondary');
$secondaryEntities = $entityManager->getRepository(SecondaryEntity::class)->findAll();
By following these steps, you can configure and use multiple databases in Symfony for your project.