CommonModel.class.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. /* *
  8. * 公共模型
  9. */
  10. namespace Common\Model;
  11. use Think\Model;
  12. class CommonModel extends Model {
  13. /**
  14. * 删除表
  15. */
  16. final public function drop_table($tablename) {
  17. $tablename = C("DB_PREFIX") . $tablename;
  18. return $this->query("DROP TABLE $tablename");
  19. }
  20. /**
  21. * 读取全部表名
  22. */
  23. final public function list_tables() {
  24. $tables = array();
  25. $data = $this->query("SHOW TABLES");
  26. foreach ($data as $k => $v) {
  27. $tables[] = $v['tables_in_' . strtolower(C("DB_NAME"))];
  28. }
  29. return $tables;
  30. }
  31. /**
  32. * 检查表是否存在
  33. * $table 不带表前缀
  34. */
  35. final public function table_exists($table) {
  36. $tables = $this->list_tables();
  37. return in_array(C("DB_PREFIX") . $table, $tables) ? true : false;
  38. }
  39. /**
  40. * 获取表字段
  41. * $table 不带表前缀
  42. */
  43. final public function get_fields($table) {
  44. $fields = array();
  45. $table = C("DB_PREFIX") . $table;
  46. $data = $this->query("SHOW COLUMNS FROM $table");
  47. foreach ($data as $v) {
  48. $fields[$v['Field']] = $v['Type'];
  49. }
  50. return $fields;
  51. }
  52. /**
  53. * 检查字段是否存在
  54. * $table 不带表前缀
  55. */
  56. final public function field_exists($table, $field) {
  57. $fields = $this->get_fields($table);
  58. return array_key_exists($field, $fields);
  59. }
  60. protected function _before_write(&$data) {
  61. }
  62. }