我正在使用Symfony3应用程序,我想建立到不同数据库的多个连接.
我一直在环顾四周,发现了有关entityManagers和DB连接的文档.我的config.yml配置如下:
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
所以现在我可以像这样访问我的数据库:
$con2 = $this->get('doctrine.dbal.other_connection');
$orders = $con2->fetchAll('SELECT * FROM orders');
但我真正需要的是配置第二个orm-mapping连接,它允许我与实体交互而不是直接处理第二个数据库.所以再次,因为文档说我在学说orm标签下添加:
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: ~
这引发了一个异常:
ParseException in Parser.php line 296:
Unable to parse at line 78 (near ” entity_managers:”).
我应该如何配置我的config.yml以允许我的第二个数据库连接的orm-mapping?我应该删除dql标签并仅在某个实体管理器标签下使用它吗?
解决方法:
试试这个:
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: ~