Switching db connection in Doctrine models
I'm researching Zend+Doctrine performance on an existing shard database system. Most of my application models connect to the main database (db1) whilst some models need to dynamically connect to different databases (db2, db3 etc.)
Of the models that do connect to开发者_StackOverflow社区 a different database, I need an easy way for it to switch connection. Is there any method in Doctrine_Record that I can override to provide a new connection instance?
No need to override anything. You simple create as many connections as you need (per default, you should have one connection that has also a name, now you have n connections with different names.
$conn1 = Doctrine_Manager::connection('mysql://username:password@localhost/database1', 'connection1');
$conn2 = Doctrine_Manager::connection('mysql://username:password@localhost/database2', 'connection2');
Different models can be bound to different connections. I always put them in the base models.
Doctrine_Manager::connection()->bindComponent('Your_Model', 'connection1');
Alternatively, you can use a method of the Connection Manager
setCurrentConnection('connection1')
This should help you:
http://www.doctrine-project.org/projects/orm/1.2/docs/manual/connections/en
精彩评论