在 CodeIgniter 中切换数据库

发布于 2024-12-07 08:05:59 字数 1868 浏览 1 评论 0原文

抱歉,如果这是一个真正的新手问题(也是一个 php 新手),但我无法从文档中弄清楚:

如果我不指定其他任何内容,我希望能够使用默认数据库,并且可以使用可以使用 $this->db 进行以下配置:

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'ion_auth';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

$db['visitorsDB']['hostname'] = 'localhost';
$db['visitorsDB']['username'] = 'root';
$db['visitorsDB']['password'] = 'root';
$db['visitorsDB']['database'] = 'visitorsDB';
$db['visitorsDB']['dbdriver'] = 'mysql';
$db['visitorsDB']['dbprefix'] = '';
$db['visitorsDB']['pconnect'] = TRUE;
$db['visitorsDB']['db_debug'] = TRUE;
$db['visitorsDB']['cache_on'] = FALSE;
$db['visitorsDB']['cachedir'] = '';
$db['visitorsDB']['char_set'] = 'utf8';
$db['visitorsDB']['dbcollat'] = 'utf8_general_ci';
$db['visitorsDB']['swap_pre'] = '';
$db['visitorsDB']['autoinit'] = TRUE;
$db['visitorsDB']['stricton'] = FALSE;

但是我想在我的模型之一中使用第二个数据库,所以我尝试了这个:

function __contruct() {
      parent::__construct();
       $this->db =  $this->load->database('visitorsDB', TRUE);
   }

   public function getAllVisitors($paramArr) {
       //$this->db =  $this->load->database('visitorsDB', TRUE);

但这不起作用。只有当我取消注释最后一行以便在方法本身中加载数据库时它才会起作用。我不明白为什么。我还尝试在类的开头声明一个新的类变量 - private $myDB; - 然后在构造函数中实例化它 - $myDB = $this->load->database('visitorsDB', TRUE);

但这也不起作用。那么如何切换整个模型的数据库呢?

Sorry if this is a real newbie question (also a php newbie), but I can't figure it out from the docs:

I want to be able to use a default database, if I don't speficy anything else, and that works fine using $this->db with the following configuration:

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'ion_auth';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

$db['visitorsDB']['hostname'] = 'localhost';
$db['visitorsDB']['username'] = 'root';
$db['visitorsDB']['password'] = 'root';
$db['visitorsDB']['database'] = 'visitorsDB';
$db['visitorsDB']['dbdriver'] = 'mysql';
$db['visitorsDB']['dbprefix'] = '';
$db['visitorsDB']['pconnect'] = TRUE;
$db['visitorsDB']['db_debug'] = TRUE;
$db['visitorsDB']['cache_on'] = FALSE;
$db['visitorsDB']['cachedir'] = '';
$db['visitorsDB']['char_set'] = 'utf8';
$db['visitorsDB']['dbcollat'] = 'utf8_general_ci';
$db['visitorsDB']['swap_pre'] = '';
$db['visitorsDB']['autoinit'] = TRUE;
$db['visitorsDB']['stricton'] = FALSE;

But then I want to use the second database in one of my models, so I tried this:

function __contruct() {
      parent::__construct();
       $this->db =  $this->load->database('visitorsDB', TRUE);
   }

   public function getAllVisitors($paramArr) {
       //$this->db =  $this->load->database('visitorsDB', TRUE);

But that doesn't work. Only if I uncomment the last line so that I load the database in the method itself will it work. I don't understand why. I also tried declaring a new class variable at the beginning of the class - private $myDB; - and then instantiating it in the constructor - $myDB = $this->load->database('visitorsDB', TRUE);

But that didn't work either. So how can I switch database for the entire model?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-12-14 08:05:59

当数据库的登录用户更改时

private $db;

function __construct() {
  parent::__construct();

  if($this->session->userdata('username') == "swaroop")
        $this->db = $this->load->database('visitorsDB', TRUE);
    else
        $this->db = $this->load->database('default', TRUE);
}

when login user with database changed

private $db;

function __construct() {
  parent::__construct();

  if($this->session->userdata('username') == "swaroop")
        $this->db = $this->load->database('visitorsDB', TRUE);
    else
        $this->db = $this->load->database('default', TRUE);
}
我还不会笑 2024-12-14 08:05:59

我不建议通过覆盖 $this->db 来选择第二个数据库。只需在您需要的模型中为您的其他数据库添加一个属性并通过它访问它即可。在你的模型中这样做:

private $myDB;

function __construct() {
    parent::__construct();
    $this->myDB = $this->load->database('visitorsDB', TRUE);
}

function getAllVisitors() {
    return $this->myDB->someMethod(); // Or use active record, etc...
}

我知道在你的问题中你说过你做了类似的事情,但我不确定你是否做得正确。这将是这样做的正确方法,我不明白为什么如果这样做就行不通。

I wouldn't recommend selecting a second database by overwriting $this->db. Just add a property for your other database in the model you need it and access it through that. In your model do this:

private $myDB;

function __construct() {
    parent::__construct();
    $this->myDB = $this->load->database('visitorsDB', TRUE);
}

function getAllVisitors() {
    return $this->myDB->someMethod(); // Or use active record, etc...
}

I know in your question you said that you did something similar but I'm not sure if you did it correctly. This would be the correct way of doing so and I don't see why it wouldn't work if it was done this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文