Productticket.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. class Productticket extends Model
  6. {
  7. // 表名
  8. protected $table = 'product_ticket';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = false;
  11. // 定义时间戳字段名
  12. protected $createTime = false;
  13. protected $updateTime = false;
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'roadtype_text',
  18. 'is_qudao_text'
  19. ];
  20. protected static function init()
  21. {
  22. self::beforeUpdate(function ($row) {
  23. $road_info = Db::name('product_road')->where('id',$row->road_id)->find();
  24. $row->roadtype = $road_info['roadtype'];
  25. });
  26. self::beforeInsert(function ($row) {
  27. $road_info = Db::name('product_road')->where('id',$row->road_id)->find();
  28. $row->roadtype = $road_info['roadtype'];
  29. });
  30. }
  31. public function getRoadtypeList()
  32. {
  33. return ['1' => __('Roadtype 1'), '2' => __('Roadtype 2'), '3' => __('Roadtype 3')];
  34. }
  35. public function getIsQudaoList()
  36. {
  37. return ['0' => __('Is_qudao 0'), '1' => __('Is_qudao 1')];
  38. }
  39. public function getRoadtypeTextAttr($value, $data)
  40. {
  41. $value = $value ? $value : (isset($data['roadtype']) ? $data['roadtype'] : '');
  42. $list = $this->getRoadtypeList();
  43. return isset($list[$value]) ? $list[$value] : '';
  44. }
  45. public function getIsQudaoTextAttr($value, $data)
  46. {
  47. $value = $value ? $value : (isset($data['is_qudao']) ? $data['is_qudao'] : '');
  48. $list = $this->getIsQudaoList();
  49. return isset($list[$value]) ? $list[$value] : '';
  50. }
  51. public function product()
  52. {
  53. return $this->belongsTo('Product', 'product_id', 'id', [], 'LEFT')->setEagerlyType(0);
  54. }
  55. public function road()
  56. {
  57. return $this->belongsTo('app\admin\model\Productroad', 'road_id', 'id', [], 'LEFT')->setEagerlyType(0);
  58. }
  59. }