我是Yii Framwork的新手,我使用Multiple-database support in Yii中的步骤来连接不同的数据库,它对我帮助很大。
但是没有加载Css,当我打开index.php时,浏览器中会显示普通的HTML内容。
在更改模块中的GetDbConnection()之后加载CSS需要进行哪些更改。
来自模特的Ad.php代码
<?php
class Ad extends MyActiveRecord
{
public $password;
public $repassword;
public function getDbConnection()
{
return self::getCCDbConnection();
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
....
}
先谢谢
答案 0 :(得分:1)
这不能解决你的css问题,但是,这是在yii中使用多个dbs的正确方法。
这是在multiple db's
中使用yii mvc
的正确方法:
假设我有多个数据库,我用它来存储网址。
我不时需要更改数据库。
所以,我有使用gii
生成的模型,最重要的是我有扩展和覆盖一些方法/函数的类。
UrlSlaveM
扩展UrlSlave
扩展CActiveRecord
默认情况下,UrlSlave
我将连接到我的第一个数据库
我插入新数据时总是使用UrlSlaveM
,以便我可以覆盖以下函数:
public function getDbConnection(){ return Yii :: app() - &gt; 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!
),