在Symfony2中与两个实体管理员一起工作 [英] Working with two entity managers in the same bundle in Symfony2
问题描述
orm:
default_entity_manager:默认
entity_managers:
电话:
连接:电费
映射:
XXDemoBundle:〜
默认值:
连接:默认
映射:
XXDemoBundle: 〜
有什么办法可以告诉我们哪个实体经理属于哪个实体经理?如果我想使用不属于默认实体经理的表,它现在崩溃。
谢谢
- 更新
这是我的连接配置:
doctrine:
dbal:
default_connection:默认
连接:
默认值:
dbname:old_project
用户:root
密码:123123
主机:1.1.1.1
端口:1
电费:
dbname:电费
用户:根
密码:123123
主机:2.2.2.2
端口:2
orm:
default_entity_manager:电费
entity_managers:
电话:
connectio n:electra
映射:
XXDemoBundle:〜
默认值:
连接:默认
映射:
XXDemoBundle:〜
为了在同一个bundle中使用多个entitymanager,你必须配置每个实体管理员的映射选项。
http: /symfony.com/doc/current/reference/configuration/doctrine.html
例如关闭配置文件
原则:
dbal:
default_connection:默认
连接:
默认值:
驱动程序:%database_driver%
主机:%database_host%
端口:%database_port%
dbname:%database_name%
用户:%database_user%
密码:%database_password%
charset:UTF8
秒:
驱动程序:%database_sqlite_driver%
主机:〜
端口:〜
dbname:%database_sqlite_shop_name%
路径:%database_sqlite_shop_name%
用户:〜
密码:〜
字符集:UTF8
orm:
auto_generate_proxy_classes:%kernel.debug %
default_entity_manager:默认
entity_managers:
默认值:
连接:默认
映射:
YourBundle:
#必须指定类型
类型:注释
#实体(相对于包路径)的目录
dir:Entity / FirstDb
#the前缀
前缀:Your\Bundle\Entity\FirstDb
shop:
connection:second
mappings:
YourBundle:
类型:注释
#连接的实体的第二个路径站在
dir:Entity / SecondDb
#the前缀
前缀:Your\Bundle\\ \\ entity\SecondDb
现在可以使用console使用--em参数
$ b $来管理数据库b例如:shop entitymanager的更新数据库
php app / console doctrine:schema:update - em = shop
从Your\Bundle\Entity\SecondDb读取映射信息
Ex:默认entitymanager的更新数据库
php app / con唯一的原则:模式:更新
从Your\Bundle\Entity\FirstDb读取映射信息
I try to work with two entity managers for the same bundle. My configuration is like this:
orm:
default_entity_manager: default
entity_managers:
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
Is there any way to tell wich entites belong to which entity manager? It crashes now if I want to work with a table which doens't belong to the default entity manager.
Thanks
- UPDATE
here is my configuration for the connection:
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: old_project
user: root
password: 123123
host: 1.1.1.1
port: 1
electra:
dbname: electra
user: root
password: 123123
host: 2.2.2.2
port: 2
orm:
default_entity_manager: electra
entity_managers:
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
For using multiple entitymanager in same bundle you have to config mapping options for each entitymanager.
http://symfony.com/doc/current/reference/configuration/doctrine.html
Exemple off config file
doctrine: dbal: default_connection: default connections: default: driver: %database_driver% host: %database_host% port: %database_port% dbname: %database_name% user: %database_user% password: %database_password% charset: UTF8 second: driver: %database_sqlite_driver% host: ~ port: ~ dbname: %database_sqlite_shop_name% path: %database_sqlite_shop_name% user: ~ password: ~ charset: UTF8 orm: auto_generate_proxy_classes: %kernel.debug% default_entity_manager: default entity_managers: default: connection: default mappings: YourBundle: # you must specify the type type: "annotation" # The directory for entity (relative to bundle path) dir: "Entity/FirstDb" #the prefix prefix: "Your\Bundle\Entity\FirstDb" shop: connection: second mappings: YourBundle: type: "annotation" #here the second path where entity for the connection stand dir: "Entity/SecondDb" #the prefix prefix: "Your\Bundle\Entity\SecondDb"
You can now use console for managing your db with the --em parameter
Ex : update database for shop entitymanager
php app/console doctrine:schema:update --em=shop
Read mapping information from Your\Bundle\Entity\SecondDb
Ex : update database for default entitymanager
php app/console doctrine:schema:update
Read mapping information from Your\Bundle\Entity\FirstDb
这篇关于在Symfony2中与两个实体管理员一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!