简体   繁体   English

Codeigniter-多个数据库连接-本地和远程

[英]Codeigniter - Multiple database connections - local and remote

I am currently building an app whereby a user will be able to enter their own personal DB connections to utilise data from their MySQL database. 我当前正在构建一个应用程序,用户可以通过该应用程序输入自己的个人数据库连接来利用其MySQL数据库中的数据。

This means I will have two DB connections - my local (managing sessions etc) and the users remote one. 这意味着我将有两个数据库连接-我的本地(管理会话等)和远程的用户。

Can anyone advise on the best way to manage these two connections? 任何人都可以建议管理这两个连接的最佳方法吗? I have looked at db groups - http://codeigniter.com/user_guide/database/connecting.html however the users db connection will be set by session vars, so I cannot put the details of the remote db in the config file. 我已经看过分贝群体- http://codeigniter.com/user_guide/database/connecting.html然而用户数据库连接将被会话瓦尔设置,所以我不能把在配置文件的远程数据库的详细信息。

I have tried manually setting a new db group within my class like: 我尝试在类中手动设置新的数据库组,例如:

        $db['foreign']['hostname'] = $this->session->userdata('hostname');
        $db['foreign']['username'] = $this->session->userdata('dbuser');
        $db['foreign']['password'] = $this->session->userdata('dbpassword');
        $db['foreign']['database'] = $this->session->userdata('dbname');
        $db['foreign']['dbdriver'] = "mysql";
        $db['foreign']['dbprefix'] = "";
        $db['foreign']['pconnect'] = FALSE;
        $db['foreign']['db_debug'] = TRUE;
        $db['foreign']['cache_on'] = FALSE;
        $db['foreign']['cachedir'] = "";
        $db['foreign']['char_set'] = "utf8";
        $db['foreign']['dbcollat'] = "utf8_general_ci";

        $foreign_db = $this->load->database('foreign', TRUE);

But i get an exception on the load line: 但是我在负载线上出现异常:

You have specified an invalid database connection group.

Can anyone advise how I can achieve this? 谁能建议我如何实现这一目标?

Many thanks, Ben. 非常感谢,本。

In order to use the group calling syntax, the group has to be defined in the config file. 为了使用组调用语法,必须在配置文件中定义组。 CI includes the ability to pass the config directly in to the database loader. CI包括将配置直接传递到数据库加载器的功能。 (Note: this might only be a CI 2+ feature) (注意:这可能仅是CI 2+功能)

You want something like this: 您想要这样的东西:

$db['hostname'] = $this->session->userdata('hostname');
$db['username'] = $this->session->userdata('dbuser');
$db['password'] = $this->session->userdata('dbpassword');
$db['database'] = $this->session->userdata('dbname');
$db['dbdriver'] = "mysql";
$db['dbprefix'] = "";
$db['pconnect'] = FALSE;
$db['db_debug'] = TRUE;
$db['cache_on'] = FALSE;
$db['cachedir'] = "";
$db['char_set'] = "utf8";
$db['dbcollat'] = "utf8_general_ci";

$foreign_db = $this->load->database($db, TRUE);

The 'foreign' group is probably not known to the system at the point you're trying to load it. 在尝试加载时,系统可能不知道“外部”组。

You shouldn't load anything in your config files; 您不应该在配置文件中加载任何内容。 that's not what config files are for, which basically is the problem. 这不是配置文件的用途,这基本上就是问题所在。

If you really need to autoload the foreign database, do it by extending the CI controller and load it in it's construct. 如果您确实需要自动加载外部数据库,请通过扩展CI控制器并将其加载到其构造中来完成。

you could use a hook to get the session var's and populate the config vars pre-connection. 您可以使用钩子获取会话变量,并填充config vars预连接。

else you could manually call the second DB class from the controllers and use the session class to define the details ? 否则,您可以从控制器手动调用第二个数据库类,并使用会话类定义详细信息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM