I'm working with a Symfony3 app and I want to set up multiple connections to different databases.
I've been looking around and I found out the documentation about entityManagers and DB connections. My config.yml is configured as follows:
config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mapping_types:
enum: string
other:
driver: pdo_mysql
host: "%database_host2%"
port: "%database_port2%"
dbname: "%database_name2%"
user: "%database_user2%"
password: "%database_password2%"
charset: UTF8
mapping_types:
enum: string
orm:
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
So now I do can access to my database like this:
$con2 = $this->get('doctrine.dbal.other_connection');
$orders = $con2->fetchAll('SELECT * FROM orders');
But what I really need is to configure a second orm-mapping connection which will allow me to interact with entities instead of dealing with the second database directly. So again as the documentation says I added under the doctrine orm label:
orm:
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
other:
connection: other
mappings:
OtherBundle: ~
This throws an exception:
ParseException in Parser.php line 296: Unable to parse at line 78 (near " entity_managers:").
How should I configure my config.yml to allow orm-mapping for my second database connection? Should I delete the dql label and use it only under a certain entity manager label?
Try this one:
doctrine:
orm:
auto_generate_proxy_classes: true
entity_managers:
default:
mappings:
AppBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
other:
mappings:
OtherBundle: ~
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments