Model 如何动态更改数据库连接
我的项目使用两个db。在配置中。yml文件Model 如何动态更改数据库连接,model,schema,database-connection,symfony-1.4,Model,Schema,Database Connection,Symfony 1.4,我的项目使用两个db。在配置中。yml文件 all: master: class: sfDoctrineDatabase param: dsn: 'mysql:host=localhost;dbname=livetloc' username: root password: console client: class: sfDoctrineDatabase param: dsn: 'mysql:host=localhost;dbname
all:
master:
class: sfDoctrineDatabase
param:
dsn: 'mysql:host=localhost;dbname=livetloc'
username: root
password: console
client:
class: sfDoctrineDatabase
param:
dsn: 'mysql:host=localhost;dbname=testloc'
username: root
password: console
然后我生成了模式和模型。现在我需要动态更改数据库连接。如果我在我的项目中编写一个新代码,我需要在testlocdb上测试它。之后,我会将其更改为liveloc db。该项目由四名成员使用。我正在向它添加新的特性,四个成员同时使用这个项目,但我使用“testloc”数据库。其他人则使用“Liveloc”数据库。在单个环境中更改特定登录用户的数据库的解决方案是什么?我在以前的项目中就有这个需要,我就是这样解决的: 在控制器中:
// we get the params in your databases.yml file for your data source "client"
$dbParams = sfContext::getInstance()->getDatabaseManager()->getDatabase('client');
$paramsArray = array(
$dbParams->getParameter('dsn'),
$dbParams->getParameter('username'),
$dbParams->getParameter('password'),
'testloc'
);
// setting "false" avoids this new connection to replace your default one
$newConnection = Doctrine_Manager::getInstance()->openConnection($params, 'name_of_connection', false);
$query = "SELECT * FROM table";
$result = $newConnection->fetchAll($query);
这并不是很酷,因为它使用了symfony上下文,但这就做到了;)。在测试环境中,我认为这很好