Thanks.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace app\admin\model\ask;
  3. use think\Model;
  4. class Thanks extends Model
  5. {
  6. // 表名
  7. protected $name = 'ask_thanks';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. // 追加属性
  14. protected $append = [
  15. 'type_text',
  16. 'paytime_text',
  17. 'status_text'
  18. ];
  19. public function getTypeList()
  20. {
  21. return ['question' => __('Question'), 'article' => __('Article'), 'answer' => __('Answer')];
  22. }
  23. public function getStatusList()
  24. {
  25. return ['created' => __('Created'), 'paid' => __('Paid')];
  26. }
  27. public function getTypeTextAttr($value, $data)
  28. {
  29. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  30. $list = $this->getTypeList();
  31. return isset($list[$value]) ? $list[$value] : '';
  32. }
  33. public function getPaytimeTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
  36. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  37. }
  38. public function getStatusTextAttr($value, $data)
  39. {
  40. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  41. $list = $this->getStatusList();
  42. return isset($list[$value]) ? $list[$value] : '';
  43. }
  44. protected function setPaytimeAttr($value)
  45. {
  46. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  47. }
  48. public function user()
  49. {
  50. return $this->belongsTo("\app\common\model\User", 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  51. }
  52. }