IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]Yii 跨数据库关联关系

    iefreer发表于 2015-07-31 18:54:52
    love 0

    要在两个数据库实例(同一个server)上进行关联数据查询,纯SQL语句如下:

    select a.*,b.* from db1.user as a,db2.log as b where a.id=b.user_id;


    要在Yii1 Model中支持这个,需要修改下user模型类的getDbConnection和tableName方法如下:

    	/**
    	 * @return CDbConnection database connection
    	 */
    	public function getDbConnection()
    	{
    		return Yii::app()->db_log;
    	}
    
    	/**
    	 * @return string the associated database table name
    	 */
    	public function tableName()
    	{
            preg_match("/dbname=([^;]+)/i", $this->dbConnection->connectionString, $matches);
            return $matches[1].'.log';
    	}

    by iefreer



沪ICP备19023445号-2号
友情链接