1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- /**
- * 动态操作学校数据库基类
- */
- class MyActiveRecord extends CActiveRecord {
- static public $schoolId = 0;
- public function getDbConnection()
- {
- if (is_cli()) {
- return $this->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;
- }
- }
|