PHP Yii:数据库在运行时连接 [英] PHP Yii: Database connect in runtime
本文介绍了PHP Yii:数据库在运行时连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在运行时使用Yii连接到第二个数据库。数据库名称将在用户登录后来自数据库表。
I would like to connect to a second database with Yii at runtime. The database name would come from a database table after the user to login.
我在教程中看到我应该这样做:
I saw in a tutorial I should do this:
$db2 = Yii::createComponent(array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => $emp['database']
));
Yii::app()->setComponent('db2',$db2);
但是当我访问 Yii :: app()-> ; db2
收到错误:
But in my controler when I access Yii::app()->db2
get the error:
未定义属性 CWebApplication.db2
Property "CWebApplication.db2" is not defined
我在做什么错了?
推荐答案
以下对我有用的东西:
Yii::app()->mongodb->setActive(false);
Yii::app()->mongodb->setServer('mongodb://localhost:27017');
Yii::app()->mongodb->setDb('db1');
Yii::app()->mongodb->setActive(true);
这篇关于PHP Yii:数据库在运行时连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文