Codeigniter 2手动数据库连接

我刚刚测试了对codeigniter 2的升级并遇到了麻烦。 我的网站使用多个数据库,一个是“系统”数据库,另一个是“内容”数据库之一。 一次只加载两个数据库,根据会话中的值,系统和任何内容都是必需的。</ p>

由于使用了会话,我连接到构造函数中的相关内容数据库 控制器通过MY_Controller中的一个函数。</ p>

不知何故,由于升级到codeigniter 2,内容数据库的连接对象没有进入使用它的模型。</ p>

IE 连接的变量是在控制器和模型以及MY_Controller和MY_Model的顶部公开的,但是当它到达MY_Model时,它不再是一个对象。</ p>

任何想法 什么会重置变量的值,因为它传递给codeigniter 2中的模型而不是codeigniter 1.7.3?</ p>
</ div>

展开原文

原文

I have just been testing an upgrade to codeigniter 2 and have hit a snag. My site uses multiple databases, one being a 'system' database and one of several 'content' databases. Only two databases are loaded at once, the system and whichever content is necessary according to a value in the session.

Because of the use of session, I connect to relevant content database in the constructor of the controllers through a function in MY_Controller.

Somehow, since upgrading to codeigniter 2, the connection object to the content database doesn't make it to the model that would use it.

I.E. the variable of the connection is public'ed at the top of the controllers and model and MY_Controller and MY_Model but by the time it has reached MY_Model, it is no longer an object.

Any ideas what would reset the value of a variable as it is passed to the models in codeigniter 2 but not codeigniter 1.7.3?

1个回答



两个数据库是否都使用相同的主机名,用户名和密码?</ p>

如果是,则这是 我们遇到的同样问题:
http://codeigniter.com/forums/viewthread/72240 / P15 / </ p>

我们通过创建具有不同用户名的mysql用户来解决它,userA可以访问dbA,而userB可以访问dbB。</ p>

当你有:</ p>


  1. $ this-&gt; dbA = $ this-&gt; load-&gt; database('dbA_config ',true); </ code> </ li>
  2. $ this-&gt; dbB = $ this-&gt; load-&gt; database('dbB_config',true); </ code> </ li>
    </ ol>

    当您运行第2行时,php会查看此连接是否已打开,问题是php只查看主机名,用户名,密码等等 你试图改变数据库PHP实际上是通过引用更新dbA。</ p>

    希望有所帮助。</ p>

    -Noah </ p>
    </ DIV>

展开原文

原文

Are both databases using same hostname, username, password?

If yes then this is the same problem we just ran into: http://codeigniter.com/forums/viewthread/72240/P15/

We solved it by creating to mysql users with different usernames, userA has access to dbA and userB has access to dbB.

What is happening is when you have:

  1. $this->dbA = $this->load->database('dbA_config', true);
  2. $this->dbB = $this->load->database('dbB_config', true);

When you run line #2 php looks to see if this connection is already open, problem is that php is only looking at the hostname, username, password so when you try to just change database PHP is actually updating dbA by reference.

Hope that helps.

-Noah

duanfeng3879
duanfeng3879 非常感谢noah,不幸的是问题只是我对OOP的稍微扭曲的理解,这在Codeigniter 2中得到了正确的实现,但在Codeigniter 1中略有不足。我正在使用公共变量,但是通过在每个控制器和模型中重新定义它们,我被覆盖了 他们的价值! 愚蠢的我。
9 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐