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 创作, 欢迎转载,但任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问,请给我留言。
  • 相关阅读:
    MySQL的四种事务隔离级别理解(new)
    深入分析ReentrantLock公平锁和非公平锁的区别 (转)
    Ubuntu 安装nginx
    Linux 文件的权限
    java Request 获得用户IP地址
    Maven profile 打包分环境加载不同的资源文件
    JQuery Ajax jsonp
    HttpClient 4.5.3 get和post请求https post
    Jenkins的安装配置
    javascript正则表达式
  • 原文地址:https://www.cnblogs.com/oshine/p/3884941.html
Copyright © 2011-2022 走看看