AssistModel.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. class AssistModel{
  3. public $sConn;
  4. public $conn;
  5. function __construct($schoolId=null){
  6. $this->conn = Yii::app()->businessDb;
  7. if($schoolId){
  8. $this->sConn = $this->getDbConnection($schoolId);
  9. }
  10. }
  11. public function getDbConnection($schoolId){
  12. $getDbConnect = BusinessDatabase::model()->find('school_id=:sid',array(':sid'=>$schoolId));
  13. $db = $getDbConnect;
  14. unset($getDbConnect);
  15. $myDbDsn = 'mysql:host='.$db->database_host.';dbname='.$db->database_name;
  16. $my_connection = new CDbConnection($myDbDsn,$db->database_user,$db->database_password);
  17. $my_connection->emulatePrepare = true;
  18. $my_connection->enableProfiling = true;
  19. $my_connection->enableParamLogging = true;
  20. $myDbDsn = null;
  21. return $my_connection;
  22. }
  23. public function isCurrentSemester($semesterId){
  24. $semesterId = (string)trim($semesterId);
  25. return $this->semester["id"] === $semesterId ? true : false;
  26. }
  27. //根据格式化语句返回最终SQL语句的非链式构造器
  28. public function orderBy($orderBy = array()){
  29. return $orderBy ? " order by ".implode(",", $orderBy) : "";
  30. }
  31. //根据格式化语句返回最终SQL语句的非链式构造器
  32. public function condition($condition = array()){
  33. return $condition ? " where ".implode(" and ", $condition) : "";
  34. }
  35. //根据格式化语句返回最终SQL语句的非链式构造器
  36. public function limit($offset, $limit){
  37. return $limit > 0 ? " limit {$offset}, {$limit}" : "";
  38. }
  39. public function paging($conn, $handle, $pageSize){
  40. $pager = new CPagination($handle->rowCount, $pageSize);
  41. $handle = $conn->createCommand($handle->queryString." limit :offset, :limit");
  42. $handle->bindValue(":offset", $pager->currentPage * $pager->pageSize);
  43. $handle->bindValue(":limit", $pager->pageSize);
  44. $rs = $handle->queryAll();
  45. return array("rs" => $rs, "pager" => $pager);
  46. }
  47. //从结果集分离出单个字段并合并成1维数组
  48. public function grouping($rs, $field, $unique = false){
  49. $group = array();
  50. if($field && $rs){
  51. foreach($rs as $key => $val){
  52. if(isset($val[$field]))
  53. $group[] = $val[$field];
  54. }
  55. if($unique === true)
  56. $group = array_unique($group);
  57. }
  58. return $group;
  59. }
  60. public function buildEmptyPagingStruct(){
  61. $rs = array(
  62. "rs" => array(),
  63. "pager" => (object)array(),
  64. );
  65. $rs["pager"]->pagesCount = 0;
  66. $rs["pager"]->rowsCount = 0;
  67. return $rs;
  68. }
  69. }