Model.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. class Model{
  3. public $sConn;
  4. public $conn;
  5. public $semester;
  6. public $schoolId;
  7. public $apiUrl;
  8. public $moduleUrl;
  9. public $moduleField;
  10. function __construct(){
  11. $this->conn = Yii::app()->businessDb;
  12. $this->sConn = $this->getDbConnection();
  13. $semesterId = Yii::app()->session['session_semester_id'];
  14. $semesterName = Yii::app()->session['session_semester_name'];
  15. $this->semester = array(
  16. "id" => $semesterId,
  17. "name" => $semesterName,
  18. );
  19. if(!Yii::app()->session['coachInfo']['school_id'])
  20. throw new CException("无法获取学校信息");
  21. $this->schoolId = Yii::app()->session['coachInfo']['school_id'];
  22. $this->apiUrl = Yii::app()->params["api"][0]["prefix"];
  23. $this->moduleUrl = $this->apiUrl.Yii::app()->params["topic_store"]["module_append"];
  24. $this->moduleField = Yii::app()->params["topic_store"]["module_field"];
  25. }
  26. public function getDbConnection(){
  27. $db = Yii::app()->session['myDatebase'];
  28. if(empty($db)) {
  29. $getDbConnect = BusinessDatabase::model()->find('school_id=:sid',array(':sid'=>Yii::app()->session['coachInfo']['school_id']));
  30. if(empty($getDbConnect)){
  31. //删除session变量
  32. Yii::app()->session->clear();
  33. header('Location:/');
  34. exit();
  35. }
  36. Yii::app()->session['myDatebase'] = $getDbConnect;
  37. $db = $getDbConnect;
  38. unset($getDbConnect);
  39. }
  40. $myDbDsn = 'mysql:host='.$db->database_host.';dbname='.$db->database_name;
  41. $my_connection = new CDbConnection($myDbDsn,$db->database_user,$db->database_password);
  42. $my_connection->emulatePrepare = true;
  43. $my_connection->enableProfiling = true;
  44. $my_connection->enableParamLogging = true;
  45. $myDbDsn = null;
  46. return $my_connection;
  47. }
  48. public function isCurrentSemester($semesterId){
  49. $semesterId = (string)trim($semesterId);
  50. return $this->semester["id"] === $semesterId ? true : false;
  51. }
  52. //根据格式化语句返回最终SQL语句的非链式构造器
  53. public function orderBy($orderBy = array()){
  54. return $orderBy ? " order by ".implode(",", $orderBy) : "";
  55. }
  56. //根据格式化语句返回最终SQL语句的非链式构造器
  57. public function condition($condition = array()){
  58. return $condition ? " where ".implode(" and ", $condition) : "";
  59. }
  60. //根据格式化语句返回最终SQL语句的非链式构造器
  61. public function limit($offset, $limit){
  62. return $limit > 0 ? " limit {$offset}, {$limit}" : "";
  63. }
  64. public function paging($conn, $handle, $pageSize){
  65. $pager = new CPagination($handle->rowCount, $pageSize);
  66. $handle = $conn->createCommand($handle->queryString." limit :offset, :limit");
  67. $handle->bindValue(":offset", $pager->currentPage * $pager->pageSize);
  68. $handle->bindValue(":limit", $pager->pageSize);
  69. $rs = $handle->queryAll();
  70. return array("rs" => $rs, "pager" => $pager);
  71. }
  72. //从结果集分离出单个字段并合并成1维数组
  73. public function grouping($rs, $field, $unique = false){
  74. $group = array();
  75. if($field && $rs){
  76. foreach($rs as $key => $val){
  77. if(isset($val[$field]))
  78. $group[] = $val[$field];
  79. }
  80. if($unique === true)
  81. $group = array_unique($group);
  82. }
  83. return $group;
  84. }
  85. public function buildEmptyPagingStruct(){
  86. $rs = array(
  87. "rs" => array(),
  88. "pager" => (object)array(),
  89. );
  90. $rs["pager"]->pagesCount = 0;
  91. $rs["pager"]->rowsCount = 0;
  92. return $rs;
  93. }
  94. }