这并不能解决您的 css 问题,但是,这是在 yii 中使用多个 db 的正确方法。
这是在中使用的正确multiple db's
方法yii mvc
:
假设我有多个数据库,我用它们来存储 url。
有时我需要更改数据库。
所以,我有使用生成的模型gii
,在此之上,我有扩展和覆盖一些方法/函数的类。
UrlSlaveM
延伸UrlSlave
到延伸CActiveRecord
默认情况下,UrlSlave
我将连接到我的第一个数据库
我总是UrlSlaveM
在插入新数据时使用,这样我就可以覆盖以下函数:
public function getDbConnection() { return Yii::app()->db1; }
这是一个完整的SlaveUrl
模型:
<?php
/**
* This is the model class for table "url".
*
* The followings are the available columns in table 'url':
* @property string $id
* @property integer $instance_id
* @property integer $website_id
* @property string $link
* @property string $title
* @property integer $created
* @property integer $updated
* @property integer $status
*/
class UrlSlave extends CActiveRecord {
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return UrlSlave the static model class
*/
public static function model($className = __CLASS__) {
return parent::model($className);
}
/**
* @return CDbConnection database connection
*/
public function getDbConnection() {
return Yii::app()->db1;
}
/**
* @return string the associated database table name
*/
public function tableName() {
return 'url';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('instance_id, website_id, link, title, created, updated, status', 'required'),
array('instance_id, website_id, created, updated, status', 'numerical', 'integerOnly' => true),
array('link, title', 'length', 'max' => 255),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, instance_id, website_id, link, title, created, updated, status', 'safe', 'on' => 'search'),
);
}
/**
* @return array relational rules.
*/
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id' => 'ID',
'instance_id' => 'Instance',
'website_id' => 'Website',
'link' => 'Link',
'title' => 'Title',
'created' => 'Created',
'updated' => 'Updated',
'status' => 'Status',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('instance_id', $this->instance_id);
$criteria->compare('website_id', $this->website_id);
$criteria->compare('link', $this->link, true);
$criteria->compare('title', $this->title, true);
$criteria->compare('created', $this->created);
$criteria->compare('updated', $this->updated);
$criteria->compare('status', $this->status);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
}
这是完整的UrlSlaveM
模型:
<?php
class UrlSlaveM extends UrlSlave {
const ACTIVE = 1;
const INACTIVE = 0;
const BANNED = -1;
public static function model($className = __CLASS__) {
return parent::model($className);
}
public function rules() {
$parent_rules = parent::rules();
$rules = array_merge(
$parent_rules, array(
array('link', 'unique'),
));
return $rules;
}
public static $server_id = 1;
public static $master_db;
public function getDbConnection() {
//echo __FUNCTION__;
//die;
//echo 111;
self::$master_db = Yii::app()->{"db" . self::$server_id};
if (self::$master_db instanceof CDbConnection) {
self::$master_db->setActive(true);
return self::$master_db;
}
else
throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.'));
}
}
现在,通过设置$server_id
为 1 或 2 或 3 ...您可以连接到另一个数据库
$server_id
请像添加数据之前一样设置值UrlSlaveM::$server_id = 2;
!
public static $server_id = 1;
public static $master_db;
另外,在主配置文件中,设置如下:
'db' => array(
'connectionString' => 'mysql:host=localhost;dbname=dvc',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
'db2' => array(
'connectionString' => 'mysql:host=localhost;dbname=dvc2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => '',
'class' => 'CDbConnection' // DO NOT FORGET THIS!
),
'db1' => array(
'connectionString' => 'mysql:host=localhost;dbname=dvc1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => '',
'class' => 'CDbConnection' // DO NOT FORGET THIS!
),