Base_model.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /**
  3. * 扩展CI的基础模型
  4. */
  5. class Base_Model extends CI_Model
  6. {
  7. //protected $table;
  8. public function __construct()
  9. {
  10. //如果没有设置表名,提示设置表名
  11. if (!isset($this->table)) {
  12. throw new Exception ('please setup $table property with this model first');
  13. }
  14. parent::__construct();
  15. }
  16. /**
  17. * 查询单条记录
  18. *
  19. * @param array $where
  20. * @param string $fields
  21. *
  22. * PS : 可根据业务场景扩展
  23. */
  24. public function get_one($where, $fields='*', $order=NULL)
  25. {
  26. if (!empty($where)) {
  27. if (is_array($where)) {
  28. foreach ($where as $key => $value) {
  29. if (is_array($value)) {
  30. $value = !empty($value) ? $value : [0];
  31. $this->db->where_in($key, $value);
  32. } else {
  33. if (strpos($key, '|') === false){
  34. $this->db->where($key, $value);
  35. } else {
  36. $this->db->group_start();
  37. $key_arr = explode('|', $key);
  38. foreach ($key_arr as $field) {
  39. if ($field && $value !== '') {
  40. $this->db->or_like($field, $value);
  41. }
  42. }
  43. $this->db->group_end();
  44. }
  45. }
  46. }
  47. } else {
  48. $this->db->where($where);
  49. }
  50. }
  51. return $this->db->select($fields)->order_by($order)->get($this->table)->row_array();
  52. }
  53. /**
  54. * 新增
  55. *
  56. * @param array $data 新增数据
  57. *
  58. * @return int $res 返回成功插入记录对应的ID
  59. */
  60. public function add($data)
  61. {
  62. $this->db->insert($this->table, $data);
  63. return $this->db->insert_id();
  64. }
  65. /**
  66. * 批量新增
  67. *
  68. * @param array $data 新增数据
  69. *
  70. * @return int $res Number of rows inserted or FALSE on failure
  71. */
  72. public function add_batch($data)
  73. {
  74. return $this->db->insert_batch($this->table, $data);
  75. }
  76. /**
  77. * 替换
  78. *
  79. * @param array $data 替换数据
  80. *
  81. * @return int $res 返回成功插入记录对应的ID
  82. */
  83. public function replace($data)
  84. {
  85. $this->db->replace($this->table, $data);
  86. return $this->db->insert_id();
  87. }
  88. /**
  89. * 更新
  90. *
  91. * @param array $data 更新字段
  92. * @param array $where 判定条件
  93. */
  94. public function update($data, $where)
  95. {
  96. if (!empty($where)) {
  97. if (is_array($where)) {
  98. foreach ($where as $key => $value) {
  99. if (is_array($value)) {
  100. $value = !empty($value) ? $value : [0];
  101. $this->db->where_in($key, $value);
  102. } else {
  103. if (strpos($key, '|') === false){
  104. $this->db->where($key, $value);
  105. } else {
  106. $this->db->group_start();
  107. $key_arr = explode('|', $key);
  108. foreach ($key_arr as $field) {
  109. if ($field && $value !== '') {
  110. $this->db->or_like($field, $value);
  111. }
  112. }
  113. $this->db->group_end();
  114. }
  115. }
  116. }
  117. } else {
  118. $this->db->where($where);
  119. }
  120. return $this->db->update($this->table, $data);
  121. }
  122. return false;
  123. }
  124. /**
  125. * 删除
  126. */
  127. public function delete($where)
  128. {
  129. if (!empty($where)) {
  130. if (is_array($where)) {
  131. foreach ($where as $key => $value) {
  132. if (is_array($value)) {
  133. $value = !empty($value) ? $value : [0];
  134. $this->db->where_in($key, $value);
  135. } else {
  136. if (strpos($key, '|') === false){
  137. $this->db->where($key, $value);
  138. } else {
  139. $this->db->group_start();
  140. $key_arr = explode('|', $key);
  141. foreach ($key_arr as $field) {
  142. if ($field && $value !== '') {
  143. $this->db->or_like($field, $value);
  144. }
  145. }
  146. $this->db->group_end();
  147. }
  148. }
  149. }
  150. } else {
  151. $this->db->where($where);
  152. }
  153. return $this->db->delete($this->table);
  154. }
  155. return false;
  156. }
  157. /**
  158. * 获取数量
  159. *
  160. * @param mixed $where
  161. * @param string $fields
  162. * @param int $limit
  163. * @param int $offset
  164. * @return array
  165. */
  166. public function get_count($where=NULL)
  167. {
  168. if(!empty($where)){
  169. if (is_array($where)) {
  170. foreach ($where as $key => $value) {
  171. if (is_array($value)) {
  172. $value = !empty($value) ? $value : [0];
  173. $this->db->where_in($key, $value);
  174. } else {
  175. if (strpos($key, '|') === false){
  176. $this->db->where($key, $value);
  177. } else {
  178. $this->db->group_start();
  179. $key_arr = explode('|', $key);
  180. foreach ($key_arr as $field) {
  181. if ($field && $value !== '') {
  182. $this->db->or_like($field, $value);
  183. }
  184. }
  185. $this->db->group_end();
  186. }
  187. }
  188. }
  189. } else {
  190. $this->db->where($where);
  191. }
  192. }
  193. return $this->db->count_all_results($this->table);
  194. }
  195. /**
  196. * 获取列表
  197. *
  198. * @param mixed $where
  199. * @param string $fields
  200. * @param int $limit
  201. * @param int $offset
  202. * @return array
  203. */
  204. public function get_list($where=NULL, $fields='*',$limit=NULL, $offset=NULL, $order=NULL, $group=NUll)
  205. {
  206. $this->db->select($fields);
  207. if(!empty($where)){
  208. if (is_array($where)) {
  209. foreach ($where as $key => $value) {
  210. if (is_array($value)) {
  211. $value = !empty($value) ? $value : [0];
  212. $this->db->where_in($key, $value);
  213. } else {
  214. if (strpos($key, '|') === false){
  215. $this->db->where($key, $value);
  216. } else {
  217. $this->db->group_start();
  218. $key_arr = explode('|', $key);
  219. foreach ($key_arr as $field) {
  220. if ($field && $value !== '') {
  221. $this->db->or_like($field, $value);
  222. }
  223. }
  224. $this->db->group_end();
  225. }
  226. }
  227. }
  228. } else {
  229. $this->db->where($where);
  230. }
  231. }
  232. if(!empty($order)){
  233. $this->db->order_by($order);
  234. }
  235. if(!empty($group)){
  236. $this->db->group_by($group);
  237. }
  238. $query = $this->db->get($this->table, $limit, $offset);
  239. return $query->result_array();
  240. }
  241. /**
  242. * 公用方法 联查
  243. *
  244. * @param array $where
  245. * @param string $fields
  246. * @param int $limit
  247. * @param int $offset
  248. * @param array $join
  249. * |- string table
  250. * |- string cond 关联条件
  251. * |- string type 联查方式 ep:'left, right inner'
  252. *
  253. * @param order
  254. * @param group
  255. * @param $alias 为主表设置别名
  256. * @return array
  257. */
  258. public function get_list_by_join($where=NULL, $fields='*',$limit=NULL, $offset=NULL, $join = NULL, $order=NULL, $group=NUll, $alias=NULL)
  259. {
  260. $this->db->select($fields);
  261. $table = $this->table;
  262. if(!empty($where)){
  263. if (is_array($where)) {
  264. foreach ($where as $key => $value) {
  265. if (is_array($value)) {
  266. $value = !empty($value) ? $value : [0];
  267. $this->db->where_in($key, $value);
  268. } else {
  269. if (strpos($key, '|') === false){
  270. $this->db->where($key, $value);
  271. } else {
  272. if (trim($value) !== '') {
  273. $this->db->group_start();
  274. $key_arr = explode('|', $key);
  275. foreach ($key_arr as $field) {
  276. if ($field) {
  277. $this->db->or_like($field, $value);
  278. }
  279. }
  280. $this->db->group_end();
  281. }
  282. }
  283. }
  284. }
  285. } else {
  286. $this->db->where($where);
  287. }
  288. }
  289. if($join !== NULL){
  290. if (count($join) == count($join, 1)) {
  291. $this->db->join($join['table'], $join['cond'], $join['type']);
  292. } else {
  293. foreach ($join as $val) {
  294. $this->db->join($val['table'], $val['cond'], $val['type']);
  295. }
  296. }
  297. }
  298. if(!empty($order)){
  299. $this->db->order_by($order);
  300. }
  301. if(!empty($group)){
  302. $this->db->group_by($group);
  303. }
  304. $this->db->limit($limit, $offset);
  305. if (!empty($alias)) {
  306. $table .= ' as '.$alias;
  307. }
  308. $query = $this->db->get($table);
  309. // var_dump($this->db->last_query());
  310. return $query->result_array();
  311. }
  312. /**
  313. * 公用方法, 多表联查
  314. *
  315. * @param array $where
  316. * @param string $fields
  317. * @param int $limit
  318. * @param int $offset
  319. * @param array $join 二维关联数组
  320. * array
  321. * |- string table
  322. * |- string cond 关联条件
  323. * |- string type 联查方式 ep:'left, right inner'
  324. * @param string $order
  325. * @param string $group
  326. * @param string $alias
  327. *
  328. * @return array
  329. */
  330. public function get_list_by_multi_join($where=NULL, $fields='*',$limit=NULL, $offset=NULL, $join = NULL, $order=NULL, $group=NUll, $alias=NULL, $is_row = false)
  331. {
  332. $this->db->select($fields);
  333. $table = $this->table;
  334. if(!empty($where)){
  335. if (is_array($where)) {
  336. foreach ($where as $key => $value) {
  337. if (is_array($value)) {
  338. $value = !empty($value) ? $value : [0];
  339. $this->db->where_in($key, $value);
  340. } else {
  341. if (strpos($key, '|') === false){
  342. $this->db->where($key, $value);
  343. } else {
  344. $this->db->group_start();
  345. $key_arr = explode('|', $key);
  346. foreach ($key_arr as $field) {
  347. if ($field && $value !== '') {
  348. $this->db->or_like($field, $value);
  349. }
  350. }
  351. $this->db->group_end();
  352. }
  353. }
  354. }
  355. } else {
  356. $this->db->where($where);
  357. }
  358. }
  359. if($join !== NULL) {
  360. foreach ($join as $val) {
  361. $this->db->join($val['table'], $val['cond'], $val['type']);
  362. }
  363. }
  364. if (!empty($alias)) {
  365. $table .= ' as '.$alias;
  366. }
  367. if(!empty($order)){
  368. $this->db->order_by($order);
  369. }
  370. if(!empty($group)){
  371. $this->db->group_by($group);
  372. }
  373. $this->db->limit($limit, $offset);
  374. $query = $this->db->get($table);
  375. $result = $is_row ? $query->row_array() : $query->result_array();
  376. // var_dump($this->db->last_query());
  377. return $result;
  378. }
  379. /* 多联查获取单个 */
  380. public function get_one_by_multi_join($where=NULL, $fields='*',$join = NULL, $order=NULL, $group=NUll, $alias=NULL)
  381. {
  382. $res = $this->get_list_by_multi_join($where, $fields, 1, 0, $join, $order, $group, $alias);
  383. return $res[0];
  384. }
  385. /* 单联查获取单个 */
  386. public function get_one_by_join($where=NULL, $fields='*',$join = NULL, $order=NULL, $group=NUll, $alias=NULL)
  387. {
  388. $res = $this->get_list_by_join($where, $fields, 1, 0, $join, $order, $group, $alias);
  389. return $res[0];
  390. }
  391. // 判断数据是否已经存在
  392. public function getDataCount($where, $id = 0) {
  393. if(!empty($where)){
  394. if (is_array($where)) {
  395. foreach ($where as $key => $value) {
  396. if (is_array($value)) {
  397. $value = !empty($value) ? $value : [0];
  398. $this->db->where_in($key, $value);
  399. } else {
  400. if (strpos($key, '|') === false){
  401. $this->db->where($key, $value);
  402. } else {
  403. $this->db->group_start();
  404. $key_arr = explode('|', $key);
  405. foreach ($key_arr as $field) {
  406. if ($field && $value !== '') {
  407. $this->db->or_like($field, $value);
  408. }
  409. }
  410. $this->db->group_end();
  411. }
  412. }
  413. }
  414. } else {
  415. $this->db->where($where);
  416. }
  417. }
  418. if (!empty($id)) {
  419. $this->db->where('id !=',$id);
  420. }
  421. $query = $this->db->get($this->table);
  422. $data = $query->row_array();
  423. if (empty($data)) {
  424. return 0;
  425. } else {
  426. return $id == $data['id'] ? 0 : 1;
  427. }
  428. }
  429. }