Productchufadi.php 1.6 KB

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