Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Idiorm切换数据库_Php_Database_Idiorm - Fatal编程技术网

Php 使用Idiorm切换数据库

Php 使用Idiorm切换数据库,php,database,idiorm,Php,Database,Idiorm,我正在尝试使用Idiorm来概括php脚本。 其思想是:用户登录到一个auth页面。凭证存储在第一个数据库中,称为“db_用户”。 对于“db_users”中存储的每个用户,我有以下字段:login/password/authorized_db。 一旦识别出用户,我想将Idiorm请求的数据库集切换到与其权限对应的新数据库集 例如: config.php $db_host = 'localhost'; $db_user = 'user1'; $db_password

我正在尝试使用Idiorm来概括php脚本。 其思想是:用户登录到一个auth页面。凭证存储在第一个数据库中,称为“db_用户”。 对于“db_users”中存储的每个用户,我有以下字段:login/password/authorized_db。 一旦识别出用户,我想将Idiorm请求的数据库集切换到与其权限对应的新数据库集

例如:

config.php 
$db_host        = 'localhost';
$db_user       = 'user1';
$db_password    = 'user1';
$db_name        = 'db_users';
Idiorm最初的配置如下:

ORM::configure("mysql:host=$db_host;dbname=$db_name");
ORM::configure('username', $db_user);
ORM::configure('password', $db_password);
ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
ORM::configure('return_result_sets', true);
最后,在login.php页面上,我尝试了类似的方法,但ORM信息没有更新,打开的数据库始终是第一个数据库用户,即db_用户,而不是授权给该用户的数据库:

if($username != '' AND $password != ''){
  $d = ORM::for_table('users')->where('username',$username)->find_one();
   if($d){
       $the_new_db = $d['authorized_db'];
       ORM::configure("mysql:host=$db_host;dbname=$the_new_db");
       do_something_in_the_new_db();
   }
}

您应该查看文档中的多个连接部分,它详细说明了如何通过命名多个连接来实现多个连接


您需要两种配置:

(一)

(二)

提出疑问:

if($username != '' AND $password != ''){
  $d = ORM::for_table('users', 'firstDb')->where('username',$username)->find_one();
   if($d){
       $d = ORM::for_table('whichever_table', 'secondDb')->where('user_id', 1)->find_one();
   }
}
ORM::configure("mysql:host=$db_host;dbname=$db_name2", null, 'secondDb');
ORM::configure('username', $db_user);
ORM::configure('password', $db_password);
ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
ORM::configure('return_result_sets', true);
if($username != '' AND $password != ''){
  $d = ORM::for_table('users', 'firstDb')->where('username',$username)->find_one();
   if($d){
       $d = ORM::for_table('whichever_table', 'secondDb')->where('user_id', 1)->find_one();
   }
}