这是main.php文件:
'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'
),
在组件的用户识别中,我有:
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;
}