You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
6.2 KiB

7 years ago
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /**
  4. *
  5. *-------------------------------------------------------------
  6. * WB_Model
  7. *-------------------------------------------------------------
  8. * CI_Model 확장합니다.
  9. */
  10. class WB_Model extends CI_Model
  11. {
  12. protected $_table = NULL;
  13. protected $_pk = NULL;
  14. protected $_status = NULL;
  15. function __construct()
  16. {
  17. parent::__construct();
  18. $this->load->database();
  19. }
  20. /**
  21. * PK값으로 검색하여 행을 반환합니다.
  22. */
  23. function get_one( $param=array())
  24. {
  25. $param['idx'] = element('idx', $param);
  26. $param['column'] = element('column', $param, $this->_pk);
  27. $param['select'] = element('select', $param, '*');
  28. $param['from'] = element('from', $param, $this->_table);
  29. $param['where'] = element('where', $param);
  30. $param['limit'] = element('limit', $param, 1);
  31. if(! $param['idx']) return FALSE;
  32. $this->db->select($param['select']);
  33. $this->db->from( $param['from'] );
  34. $this->db->where( $param['column'], $param['idx'] );
  35. // WHERE 조건이 들어있을경우
  36. if( is_array($param['where']) && !empty($param['where']) && count($param['where']) > 0 )
  37. {
  38. foreach($param['where'] as $key => $val)
  39. {
  40. $this->db->where($key, $val);
  41. }
  42. }
  43. // 기본적으로 한줄만 가져온다.
  44. $this->db->limit( $param['limit'] );
  45. $result = $this->db->get();
  46. if( $result->num_rows() > 0 )
  47. {
  48. return $result->row_array();
  49. }
  50. else
  51. {
  52. return FALSE;
  53. }
  54. }
  55. function get_list( $param = array() )
  56. {
  57. $param['select'] = ( isset($param['select']) && $param['select'] ) ? $param['select'] : "*";
  58. $param['from'] = ( isset($param['from']) && $param['from'] ) ? $param['from'] : $this->_table;
  59. $param['join'] = ( isset($param['join']) && $param['join'] && is_array($param['join']) ) ? $param['join'] : NULL;
  60. $param['page'] = ( isset($param['page']) && $param['page'] ) ? $param['page'] : 1;
  61. $param['page_rows'] = ( isset($param['page_rows']) && $param['page_rows'] ) ? $param['page_rows'] : 15;
  62. $param['order_by'] = ( isset($param['order_by']) && $param['order_by'] ) ? $param['order_by'] : $this->_pk . " DESC";
  63. $param['where'] = ( isset($param['where']) && $param['where'] && is_array($param['where']) ) ? $param['where'] : NULL;
  64. $param['where_in'] = ( isset($param['where_in']) && $param['where_in'] && is_array($param['where_in']) ) ? $param['where_in'] : NULL;
  65. $param['sc'] = ( isset($param['sc']) && $param['sc'] ) ? $param['sc'] : NULL;
  66. $param['st'] = ( isset($param['st']) && $param['st'] ) ? $param['st'] : NULL;
  67. $param['limit'] = ( isset($param['limit']) && $param['limit'] ) ? $param['limit'] : FALSE;
  68. $param['start'] = ( $param['page'] -1 ) * $param['page_rows'];
  69. $this->db->select("SQL_CALC_FOUND_ROWS " . $param['select'], false );
  70. $this->db->from( $param['from'] );
  71. // 사용자 정의 WHERE 문 처리
  72. if( $param['where'] && is_array($param['where']) )
  73. {
  74. foreach($param['where'] as $key=>$val)
  75. {
  76. $this->db->where($key, $val);
  77. }
  78. }
  79. // 사용자 정의 WHERE_IN 문 처리
  80. if( $param['where_in'] && is_array($param['where_in']) )
  81. {
  82. foreach($param['where_in'] as $key=>$val)
  83. {
  84. $this->db->where_in($key, $val);
  85. }
  86. }
  87. // 사용자 정의 WHERE 문 처리
  88. if( $param['join'] && is_array($param['join']) )
  89. {
  90. foreach($param['join'] as $array)
  91. {
  92. $this->db->join($array[0], $array[1], $array[2]);
  93. }
  94. }
  95. // 검색어 처리
  96. if( $param['sc'] != NULL && $param['st'] !=NULL )
  97. {
  98. // 띄어쓰기로 분리해서 각각 like를 걸어준다.
  99. $st = explode(" ", $param['st']);
  100. foreach($st as $searchs)
  101. {
  102. $this->db->like($param['sc'], $searchs);
  103. }
  104. }
  105. if($param['limit'] === TRUE)
  106. {
  107. $this->db->limit( $param['page_rows'], $param['start'] );
  108. }
  109. $this->db->order_by( $param['order_by'] );
  110. $result = $this->db->get();
  111. if(! $result ) {
  112. echo "ERROR : ".$this->db->error()['message'].PHP_EOL."<br>";
  113. echo "QUERY : ".$this->db->last_query();
  114. }
  115. if(IS_TEST) {
  116. $return['query'] = $this->db->last_query();
  117. }
  118. $return['list'] = $result->result_array();
  119. $result = $this->db->query("SELECT FOUND_ROWS() AS `cnt`");
  120. $return['total_count'] = (int) $result->row(0)->cnt;
  121. $return['total_page'] = ceil($return['total_count'] / $param['page_rows'] );
  122. $num = 0;
  123. foreach($return['list'] as &$row)
  124. {
  125. $row['nums'] = $return['total_count'] - $num - $param['start'];
  126. $num++;
  127. }
  128. return $return;
  129. }
  130. function create( $param = NULL )
  131. {
  132. foreach( $param as $k => $v )
  133. {
  134. $this->db->set( $k, $v );
  135. }
  136. $this->db->from($this->_table);
  137. $this->db->insert();
  138. $pk = $this->db->insert_id();
  139. return $this->get_one($pk);
  140. }
  141. function update( $param = NULL, $idx = NULL , $debug = FALSE)
  142. {
  143. foreach( $param as $k => $v )
  144. {
  145. $this->db->set( $k, $v );
  146. }
  147. $this->db->from($this->_table);
  148. $this->db->where($this->_pk, $idx);
  149. $this->db->update();
  150. if( $debug ) {
  151. return $this->db->last_query();
  152. }
  153. return $this->get_one($idx);
  154. }
  155. function update_batch($data)
  156. {
  157. return $this->db->update_batch($this->_table, $data, $this->_pk);
  158. }
  159. function insert_batch($data)
  160. {
  161. return $this->db->insert_batch($this->_table, $data);
  162. }
  163. function delete( $idx )
  164. {
  165. $this->db->where($this->_pk, $idx);
  166. $this->db->set($this->_status, "N");
  167. $this->db->update($this->_table);
  168. }
  169. }