Codeigniter - 我希望为我的一个控制器和一个模型使用/连接到不同的数据库 [英] Codeigniter - I am looking to use/connect to a different database for one of my controllers and one model

查看:17
本文介绍了Codeigniter - 我希望为我的一个控制器和一个模型使用/连接到不同的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望为我的一个控制器和一个模型使用/连接到不同的数据库.因为在 CI 论坛上我没有得到任何回应,所以我发布了这个消息.

I am looking to use/connect to a different database for one of my controllers and one model. I posted this hear since on the forums of CI I am getting no response.

我在database.php中添加了这个:

I added this in database.php:

$db['tdb']['hostname'] = "localhost";//localhost

$db['tdb']['username'] = "username";//root

$db['tdb']['password'] = "password";//empty

$db['tdb']['database'] = "databasename";

$db['tdb']['dbdriver'] = "mysql";

$db['tdb']['dbprefix'] = "";

$db['tdb']['pconnect'] = FALSE;

$db['tdb']['db_debug'] = FALSE;

$db['tdb']['cache_on'] = FALSE;

$db['tdb']['cachedir'] = "";

$db['tdb']['char_set'] = "utf8";

$db['tdb']['dbcollat'] = "utf8_general_ci";

这是我的模型:

<?php

class Tadmin_model extends Model{

    function Tadmin_model(){

        parent::Model();

        $tdb = $this->load->database('tdb', TRUE);            
    }

    function FInsert($usernames){

        $query = $tdb->query("SELECT * FROM following");

        return $query->row();
    }    
}

?>

这是我的控制器的开始:

And this is the start of my controller:

<?php

class Tadmin extends Controller{

    function Tradmin(){

        parent::Controller();

        $this->load->model('tadmin_model');

我收到此错误:

遇到 PHP 错误

严重性:注意

消息:未定义变量:tdb

Message: Undefined variable: tdb

文件名:models/tadmin_model.php

Filename: models/tadmin_model.php

行号:...

致命错误:调用成员函数query() 在非对象上/blablabla/tadmin_model.php 上线...

Fatal error: Call to a member function query() on a non-object in /blablabla/tadmin_model.php on line ...

我在这里做错了什么?

推荐答案

您的问题不在于 CodeIgniter 的数据库函数的使用,而在于类中的变量范围.当您加载模型时,您正在连接到数据库并将结果分配给模型构造函数中的局部变量.当任何函数完成时,局部变量将被丢弃.稍后您尝试对已经被丢弃的 $tdb 变量调用 query() 方法并得到一个错误.

Your issue here isn't with the use of CodeIgniter's database functions, but with variable scoping in classes. When you load your model you're connecting to the database and assigning the result to a local variable in the model's constructor. When any function finishes, the local variables are discarded. Later you try to call the query() method on the $tdb variable which has already been thrown away and get an error.

您需要将 $this->load->database() 的结果存储在构造函数和方法都可以使用的位置.您可以将 $this->load->database() 调用移动到控制器方法中,并在每次在模型上调用 Tradmin 方法时连接到其他数据库.

You need to store the results of $this->load->database() in a location which is available to both the constructor and the method. You could move the $this->load->database() call into the controller method and connect to the other database every time you call the Tradmin method on your model.

如果您想让 $tdb 可用于模型中的所有方法,另一种方法是在类上使用成员变量,它看起来像这样....

The other way if you wanted to make $tdb available to all methods in the model is to use a member variable on the class, which would look like this....

<?php

class Tadmin_model extends Model{
    var $tdb;

    function Tadmin_model(){

        parent::Model();

        $this->tdb = $this->load->database('tdb', TRUE);            
    }

    function FInsert($usernames){

        $query = $this->tdb->query("SELECT * FROM following");

        return $query->row();
    }    
}

?>

希望有所帮助.

吉姆.

这篇关于Codeigniter - 我希望为我的一个控制器和一个模型使用/连接到不同的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆