0

我是 Yii 框架的新手,我已经使用 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);
    }
    ....

}

提前致谢

4

1 回答 1

1

这并不能解决您的 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!
        ),
于 2013-09-10T09:40:36.310 回答