123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- class Model{
- public $sConn;
- public $conn;
- public $semester;
- public $schoolId;
- public $apiUrl;
- public $moduleUrl;
- public $moduleField;
- function __construct(){
- $this->conn = Yii::app()->businessDb;
- $this->sConn = $this->getDbConnection();
-
- $semesterId = Yii::app()->session['session_semester_id'];
- $semesterName = Yii::app()->session['session_semester_name'];
-
- $this->semester = array(
- "id" => $semesterId,
- "name" => $semesterName,
- );
-
- if(!Yii::app()->session['coachInfo']['school_id'])
- throw new CException("无法获取学校信息");
-
- $this->schoolId = Yii::app()->session['coachInfo']['school_id'];
-
- $this->apiUrl = Yii::app()->params["api"][0]["prefix"];
- $this->moduleUrl = $this->apiUrl.Yii::app()->params["topic_store"]["module_append"];
- $this->moduleField = Yii::app()->params["topic_store"]["module_field"];
- }
-
- public function getDbConnection(){
- $db = Yii::app()->session['myDatebase'];
-
- if(empty($db)) {
- $getDbConnect = BusinessDatabase::model()->find('school_id=:sid',array(':sid'=>Yii::app()->session['coachInfo']['school_id']));
- if(empty($getDbConnect)){
- //删除session变量
- Yii::app()->session->clear();
- header('Location:/');
- exit();
- }
-
- Yii::app()->session['myDatebase'] = $getDbConnect;
- $db = $getDbConnect;
-
- unset($getDbConnect);
- }
- $myDbDsn = 'mysql:host='.$db->database_host.';dbname='.$db->database_name;
- $my_connection = new CDbConnection($myDbDsn,$db->database_user,$db->database_password);
- $my_connection->emulatePrepare = true;
- $my_connection->enableProfiling = true;
- $my_connection->enableParamLogging = true;
-
- $myDbDsn = null;
-
- return $my_connection;
- }
-
- public function isCurrentSemester($semesterId){
- $semesterId = (string)trim($semesterId);
- return $this->semester["id"] === $semesterId ? true : false;
- }
-
- //根据格式化语句返回最终SQL语句的非链式构造器
- public function orderBy($orderBy = array()){
- return $orderBy ? " order by ".implode(",", $orderBy) : "";
- }
-
- //根据格式化语句返回最终SQL语句的非链式构造器
- public function condition($condition = array()){
- return $condition ? " where ".implode(" and ", $condition) : "";
- }
-
- //根据格式化语句返回最终SQL语句的非链式构造器
- public function limit($offset, $limit){
- return $limit > 0 ? " limit {$offset}, {$limit}" : "";
- }
-
- public function paging($conn, $handle, $pageSize){
- $pager = new CPagination($handle->rowCount, $pageSize);
- $handle = $conn->createCommand($handle->queryString." limit :offset, :limit");
- $handle->bindValue(":offset", $pager->currentPage * $pager->pageSize);
- $handle->bindValue(":limit", $pager->pageSize);
- $rs = $handle->queryAll();
-
- return array("rs" => $rs, "pager" => $pager);
- }
-
- //从结果集分离出单个字段并合并成1维数组
- public function grouping($rs, $field, $unique = false){
- $group = array();
-
- if($field && $rs){
- foreach($rs as $key => $val){
- if(isset($val[$field]))
- $group[] = $val[$field];
- }
-
- if($unique === true)
- $group = array_unique($group);
- }
-
- return $group;
- }
-
- public function buildEmptyPagingStruct(){
- $rs = array(
- "rs" => array(),
- "pager" => (object)array(),
- );
- $rs["pager"]->pagesCount = 0;
- $rs["pager"]->rowsCount = 0;
-
- return $rs;
- }
- }
|