Hotel.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class Hotel extends Model
  5. {
  6. // 表名
  7. protected $table = 'hotel';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = false;
  10. // 定义时间戳字段名
  11. protected $createTime = false;
  12. protected $updateTime = false;
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'is_home_text',
  17. 'status_text',
  18. 'create_time_text',
  19. 'update_time_text'
  20. ];
  21. protected static function init()
  22. {
  23. self::afterInsert(function ($row) {
  24. $pk = $row->getPk();
  25. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  26. });
  27. }
  28. public function getIsHomeList()
  29. {
  30. return ['1' => __('Is_home 1'), '0' => __('Is_home 0')];
  31. }
  32. public function getStatusList()
  33. {
  34. return ['1' => __('Status 1'), '0' => __('Status 0')];
  35. }
  36. public function getIsHomeTextAttr($value, $data)
  37. {
  38. $value = $value ? $value : (isset($data['is_home']) ? $data['is_home'] : '');
  39. $list = $this->getIsHomeList();
  40. return isset($list[$value]) ? $list[$value] : '';
  41. }
  42. public function getStatusTextAttr($value, $data)
  43. {
  44. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  45. $list = $this->getStatusList();
  46. return isset($list[$value]) ? $list[$value] : '';
  47. }
  48. public function getCreateTimeTextAttr($value, $data)
  49. {
  50. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  51. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  52. }
  53. public function getUpdateTimeTextAttr($value, $data)
  54. {
  55. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  56. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  57. }
  58. protected function setCreateTimeAttr($value)
  59. {
  60. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  61. }
  62. protected function setUpdateTimeAttr($value)
  63. {
  64. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  65. }
  66. }