zoukankan      html  css  js  c++  java
  • Yii ExtendedActiveRecord 增强版 ActiveRecord 增加多数据库连接绑定功能

    ExtendedActiveRecord 继承自 CActiveRecord,因此基础功能与 CActiveRecord 无异

    为添加对多数据库连接的支持,增加了对 connectionName() 方法的回调,用法跟已有的 tableName() 方法一致,返回数据库连接组件名称的字符串。

    如果不定义该方法,则使用默认数据库连接(db)

    源码如下:

    class ExtendedActiveRecord extends CActiveRecord
    {
        public static $db = array();
    
        /**
         * @return CDbConnection
         * @throws CDbException
         */
        public function getDbConnection()
        {
            $componentName = $this->connectionName();
            if (isset(self::$db[$componentName])) {
                return self::$db[$componentName];
            } else {
                self::$db[$componentName] = Yii::app()->getComponent($componentName);
                if (self::$db[$componentName] instanceof CDbConnection)
                    return self::$db[$componentName];
                else {
                    $message = 'Active Record keyword requires a "' . $componentName . '" CDbConnection application component.';
                    Yii::log($message, CLogger::LEVEL_ERROR, 'extended');
                    throw new CDbException(Yii::t('yii', $message));
                }
            }
        }
    
        public function connectionName()
        {
            return 'db';
        }
    }
    

    实例:

    class SomeModelClass extends ExtendedActiveRecord
    {
        ......
    
        public function connectionName() {
            return 'some-db-connection';
        }
    
        ......
    }
    

      

    作者: oShine.Q
    出处:http://www.cnblogs.com/oshine/
    作品oShine.Q 创作, 欢迎转载,但任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问,请给我留言。
  • 相关阅读:
    Linux下配置APACHE支持PHP环境
    mysql 管理脚本
    RAC迁移至单机考虑几大因素
    mysql配置文件my.cnf模板
    hadoop 日常问题汇总(持续更新)
    Redis配置文件
    jquery操作select(增加,删除,清空)
    mybatis异常
    Elasticsearch 之 query与filter区别
    在elasticsearch里如何高效的使用filter
  • 原文地址:https://www.cnblogs.com/oshine/p/3884941.html
Copyright © 2011-2022 走看看