0

我正在寻找一种在 Cakephp 中创建变量数据库的方法。我正在创建一个网站,其中数据库设置(用户、密码、db)取决于登录的用户。数据库设置来自另一个数据库。提前致谢,

亚伦

- 回答

感谢您的快速回复。我知道如何在数据库之间切换,但数据库设置必须是可变的。例子

   $db = $this->DB_SET['DB_SET']['db']; //from model DB_SET the database 
    $db_login= $this->DB_SET['DB_SET']['login'];
    $db_host= $this->DB_SET['DB_SET']['host'];
    $db_pass= $this->DB_SET['DB_SET']['pass'];
    var $db2 = array(
            'driver' => 'mysql',
            'persistent' => false,
            'host' => $db_host,
            'login' => $db_login,
            'password' => $db_pass,
            'database' => $db,
            'prefix' => '',
            'encoding' => 'UTF8',
            'port' => '',
        );
4

1 回答 1

2

如果您希望即时创建数据库连接,您可能需要使用ConnectionManager::create().

// get logged in user's id and create a connection for them
$id = $this->Auth->user('id');
ConnectionManager::create("user_$id_db", array(
  'driver' => 'mysql',
  'persistent' => false,
  'host' => $db_host,
  'login' => $db_login,
  'password' => $db_pass,
  'database' => $db,
  'prefix' => '',
  'encoding' => 'UTF8',
  'port' => '',
));

然后设置您的模型以在您准备好时使用它。

// get logged in user's id and use their connection
$id = $this->Auth->user('id');
$this->Model->setDataSource("user_$id_db");
于 2012-05-31T14:10:54.013 回答