BaseModel.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace addons\exam\model;
  3. use addons\exam\traits\ModelExtend;
  4. use think\Model;
  5. /** 基础模型 */
  6. class BaseModel extends Model
  7. {
  8. use ModelExtend;
  9. protected static function init()
  10. {
  11. parent::init();
  12. self::loadCommonFile();
  13. }
  14. /**
  15. * 加载公共函数库文件
  16. */
  17. protected static function loadCommonFile()
  18. {
  19. require_once ROOT_PATH . 'addons/exam/helper.php';
  20. }
  21. public function getCreateTimeTextAttr($value, $data)
  22. {
  23. $value = $value ?: ($data['createtime'] ?? '');
  24. return is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value;
  25. }
  26. // +----------------------------------------------------------------------
  27. // 预加载模型关联(仅返回部分数据)
  28. // +----------------------------------------------------------------------
  29. public static function withSimpleUser()
  30. {
  31. return function ($query) {
  32. return $query->field('id,nickname,avatar');
  33. };
  34. }
  35. public static function withSimpleCate()
  36. {
  37. return function ($query) {
  38. return $query->field('id,name');
  39. };
  40. }
  41. public static function withSimplePaper()
  42. {
  43. return function ($query) {
  44. return $query->field('id,title');
  45. };
  46. }
  47. public static function withSimpleRoom()
  48. {
  49. return function ($query) {
  50. return $query->field('id,name');
  51. };
  52. }
  53. }