Symfony3:如何设置多个连接?
Symfony3: How to set multiple connections?
我正在使用 Symfony3 应用程序,我想建立到不同数据库的多个连接。
我四处寻找,找到了有关 entityManagers 和数据库连接的文档。我的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 映射连接,这将允许我与实体交互,而不是直接处理第二个数据库。因此,正如文档所说,我在 doctrine 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 映射?我是否应该删除dql标签并仅在某个实体管理器标签下使用它?
检查 doc here 以获取有关学说的配置的完整参考。
检查dql函数等的缩进
可能我们正在使用 Shortened Configuration Syntax,其中所有可用选项都可以直接放在 doctrine.orm 配置级别下。
希望对您有所帮助
试试这个:
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: ~
我解决了这个问题。感谢您的回答,因为它们几乎就是解决方案。我只需要在正确的级别使用标签。
以防万一它对某人有用,我将post我所做的:
config.yml
orm:
entity_managers:
default:
mappings:
AppBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
other:
mappings:
OtherBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_generate_proxy_classes: "%kernel.debug%"
一旦创建了(其他)实体管理器,您就可以像使用默认 em 一样在控制器中使用它,只需指定您正在使用的实体管理器名称即可。
$orders = $this->get('doctrine')
->getRepository('OtherBundle:Orders', 'other')
->findAll();
现在你可以开始了。
感谢您的帮助。
我正在使用 Symfony3 应用程序,我想建立到不同数据库的多个连接。
我四处寻找,找到了有关 entityManagers 和数据库连接的文档。我的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 映射连接,这将允许我与实体交互,而不是直接处理第二个数据库。因此,正如文档所说,我在 doctrine 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 映射?我是否应该删除dql标签并仅在某个实体管理器标签下使用它?
检查 doc here 以获取有关学说的配置的完整参考。
检查dql函数等的缩进
可能我们正在使用 Shortened Configuration Syntax,其中所有可用选项都可以直接放在 doctrine.orm 配置级别下。
希望对您有所帮助
试试这个:
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: ~
我解决了这个问题。感谢您的回答,因为它们几乎就是解决方案。我只需要在正确的级别使用标签。
以防万一它对某人有用,我将post我所做的:
config.yml
orm:
entity_managers:
default:
mappings:
AppBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
other:
mappings:
OtherBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_generate_proxy_classes: "%kernel.debug%"
一旦创建了(其他)实体管理器,您就可以像使用默认 em 一样在控制器中使用它,只需指定您正在使用的实体管理器名称即可。
$orders = $this->get('doctrine')
->getRepository('OtherBundle:Orders', 'other')
->findAll();
现在你可以开始了。
感谢您的帮助。