1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- class AssistModel{
- public $sConn;
- public $conn;
- function __construct($schoolId=null){
- $this->conn = Yii::app()->businessDb;
- if($schoolId){
- $this->sConn = $this->getDbConnection($schoolId);
- }
- }
- public function getDbConnection($schoolId){
- $getDbConnect = BusinessDatabase::model()->find('school_id=:sid',array(':sid'=>$schoolId));
- $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;
- }
- }
|