AR 使用其它数据库进行操作

config.php里加多配置

'db2'=>array(
			'class'=>'CDbConnection',
		),

AR中覆盖方法,设置其它的数据库

特别需要注意 $odb这个独立的处理,否则在连续操作的过层中,会有数据库链接变量冲突的相关问题。

	public  static $odb=null;
        public function getDbConnection()
	{
		if(self::$odb!==null)
			return self::$odb;
		else
		{
			self::$odb=Yii::app()->getComponent('db2');
			if(self::$odb instanceof CDbConnection)
				return self::$odb;
			else
				throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
		}
	}

转发请注明出处http://blog.martoo.cn
如有漏缺,请联系我 QQ 243008827

《AR 使用其它数据库进行操作》有4个想法

  1. Thanks for your marvelous posting! I quite enjoyed reading it, you’re a great author. I will remember to bookmark your blog and will often come back from now on. I want to encourage yourself to continue your great writing, have a nice day!

发表评论

电子邮件地址不会被公开。 必填项已用*标注