Gift.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class Gift extends Model
  5. {
  6. // 表名
  7. protected $name = 'gift';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'status_text',
  17. 'wallettype_text'
  18. ];
  19. protected static function init()
  20. {
  21. //两个毫无用处的字段,没有还不行
  22. self::afterInsert(function ($row){
  23. $data = [
  24. 'no' => $row['id'],
  25. 'price' => $row['value'],
  26. ];
  27. db('gift')->where('id',$row['id'])->update($data);
  28. });
  29. self::beforeUpdate(function ($row) {
  30. $changed = $row->getChangedData();
  31. //如果有修改密码
  32. if (isset($changed['value'])) {
  33. $row->price = $changed['value'];
  34. }
  35. });
  36. }
  37. public function getStatusList()
  38. {
  39. return ['0' => __('Status 0'), '1' => __('Status 1')];
  40. }
  41. public function getWallettypeList()
  42. {
  43. return ['1' => __('Wallettype 1'), '2' => __('Wallettype 2')];
  44. }
  45. public function getStatusTextAttr($value, $data)
  46. {
  47. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  48. $list = $this->getStatusList();
  49. return isset($list[$value]) ? $list[$value] : '';
  50. }
  51. public function getWallettypeTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : (isset($data['wallettype']) ? $data['wallettype'] : '');
  54. $list = $this->getWallettypeList();
  55. return isset($list[$value]) ? $list[$value] : '';
  56. }
  57. public function gifttype()
  58. {
  59. return $this->belongsTo('Gifttype', 'type', 'id', [], 'LEFT')->setEagerlyType(0);
  60. }
  61. }