Yii-PHP框架中的多个数据库连接
这是main.php文件:Yii-PHP框架中的多个数据库连接,php,yii,Php,Yii,这是main.php文件: 'db' => array( 'connectionString' => 'mysql:host=MYHOST;dbname=MYDB', 'emulatePrepare' => true, 'username' => 'MYUSER', 'password' => 'MYPASS', 'charset'
'db' => array(
'connectionString' => 'mysql:host=MYHOST;dbname=MYDB',
'emulatePrepare' => true,
'username' => 'MYUSER',
'password' => 'MYPASS',
'charset' => 'utf8',
),
'dbanother' => array(
'connectionString' => 'mysql:host=MYHOST;dbname=MYDB2',
'emulatePrepare' => true,
'username' => 'MYUSER2',
'password' => 'MYPASS2',
'charset' => 'utf8',
'class' => 'CDbConnection'
),
在来自组件的UserIdentity中,我有以下内容:
public function authenticate() {
.........
$loggedInUser = User::model()->find("username = :username", array("username" => $this->username));
......
}
在User模型中,我想使用MYDB2数据库中的表用户:
class User extends CActiveRecord {
private $connection_db2;
/**
* @see db connections from config/main.php
*/
public function __construct(){
$this->connection_db2= Yii::app()->dbanother;
}
public static function model($className=__CLASS__){
return parent::model($className);
}
public function tableName() {
return Yii::app()->dbanother->users;
// here i want to declare That i want to use the table **users**
}
.....
}
目前我得到的是:
未定义属性“CDbConnection.users”
你能帮我吗?thx更简单:)只需重写类中的getDbConnection()
方法:
public function getDbConnection() {
return Yii::app()->dbanother;
}
发生此错误的文件、行和代码是什么?(在错误页面打印
$file
和$line
变量时,堆栈也位于$trace
protected\models\User.php(36):CComponent->\uu get('users')中)好的,现在我得到了这个:致命错误:无法在第395行的C:\xampp\htdocs\YII\framework\db\ar\CActiveRecord.php中实例化抽象类CActiveRecord。您能帮我做一下吗:D?尝试从类中删除\uu construct
函数。现在不再需要它,因为您没有调用父::\uu construct()
从内部来看,它可能会导致类似这样的问题。在我的类user中,我只有2个函数:公共函数getDbConnection(){return Yii::app()->dbother;}公共函数tableName(){return'users';},我得到了前面提到的错误