cliConn(); }else{ return $this->webConn(); } } /** * client连接 * @return CDbConnection */ public function cliConn() { $my_connection = null; if (self::$schoolId) { $getMyDate = BusinessDatabase::model()->find('school_id=:sid',array(':sid'=>self::$schoolId)); $myDbDsn = 'mysql:host='.$getMyDate->database_host.';dbname='.$getMyDate->database_name; $my_connection = new CDbConnection($myDbDsn,$getMyDate->database_user,$getMyDate->database_password); if (!$my_connection) { throw new Exception('数据库连接失败'); } $my_connection->emulatePrepare = true; $my_connection->enableProfiling = true; $my_connection->enableParamLogging = true; $my_connection->charset = 'utf8'; $myDbDsn = null; } return $my_connection; } /** * web连接 * @return CDbConnection */ public function webConn() { $getMyDate = Yii::app()->session['myDatebase']; if (empty($getMyDate)) { $getDbConnect = BusinessDatabase::model()->find('school_id=:sid',array(':sid'=>Yii::app()->session['coachInfo']['school_id'])); if (empty($getDbConnect)) { //Yii::app()->jump->error('登录异常请联系管理员!',$this->createUrl('login/index')); //删除session变量 Yii::app()->session->clear(); header('Location:/'); exit(); } Yii::app()->session['myDatebase'] = $getDbConnect; $getMyDate = $getDbConnect; unset($getDbConnect); } $myDbDsn = 'mysql:host='.$getMyDate->database_host.';dbname='.$getMyDate->database_name; $my_connection = new CDbConnection($myDbDsn,$getMyDate->database_user,$getMyDate->database_password); //$my_connection->active = true; //$my_connection = new CDbConnection('mysql:host=127.0.0.1:3307;dbname=spider_student','spider','wt24cuzGZ8HhEFSw'); //$my_connection = new CDbConnection('mysql:host=192.168.1.232:3306;dbname=school','root','lc12345'); $my_connection->emulatePrepare = true; $my_connection->enableProfiling = true; $my_connection->enableParamLogging = true; $my_connection->charset = 'utf8'; $myDbDsn = null; return $my_connection; } }