Bez popisu
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.

Modelbase.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 公共模型
  13. // +----------------------------------------------------------------------
  14. namespace app\common\model;
  15. use think\Db;
  16. use think\facade\Config;
  17. use think\Model;
  18. /**
  19. * 公共模型
  20. */
  21. class Modelbase extends Model
  22. {
  23. /**
  24. * 删除表
  25. * @param string $tablename 不带表前缀的表名
  26. * @return type
  27. */
  28. public function drop_table($table)
  29. {
  30. $table = Config::get("database.prefix") . strtolower($table);
  31. return Db::query("DROP TABLE $table");
  32. }
  33. /**
  34. * 检查表是否存在
  35. * $table 不带表前缀
  36. */
  37. public function table_exists($table)
  38. {
  39. $table = Config::get("database.prefix") . strtolower($table);
  40. if (true == Db::query("SHOW TABLES LIKE '{$table}'")) {
  41. return true;
  42. } else {
  43. return false;
  44. }
  45. }
  46. /**
  47. * 检查字段是否存在
  48. * $table 不带表前缀
  49. */
  50. public function field_exists($table, $field)
  51. {
  52. $fields = $this->get_fields($table);
  53. return array_key_exists($field, $fields);
  54. }
  55. /**
  56. * 获取表字段
  57. * $table 不带表前缀
  58. */
  59. public function get_fields($table)
  60. {
  61. $fields = array();
  62. $table = Config::get("database.prefix") . strtolower($table);
  63. $data = Db::query("SHOW COLUMNS FROM $table");
  64. foreach ($data as $v) {
  65. $fields[$v['Field']] = $v['Type'];
  66. }
  67. return $fields;
  68. }
  69. }