ProductMarket.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Request;
  5. class ProductMarket extends Model
  6. {
  7. // 表名
  8. protected $table = 'product_market';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'create_time';
  13. protected $updateTime = 'update_time';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'create_time_text',
  18. 'update_time_text'
  19. ];
  20. //状态 normal, hidden
  21. const Hidden = 0;
  22. const Normal = 1;
  23. //产品
  24. public function products()
  25. {
  26. $header = Request::instance()->header('Accept-Language');
  27. $lan = !empty($header)? substr($header, 0, 2):'zh' ;
  28. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->field('id,'.$lan .'_name as name,thum');
  29. }
  30. //寄售
  31. public function producttransfer()
  32. {
  33. $order = 'price asc';
  34. if(input('post.sort') == 1) $order = 'price desc';
  35. return $this->hasMany('ProductTransfer', 'product_id', 'product_id', [], 'LEFT')->order($order)->where('status', self::Normal);
  36. }
  37. public function getCreateTimeTextAttr($value, $data)
  38. {
  39. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  40. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  41. }
  42. public function getUpdateTimeTextAttr($value, $data)
  43. {
  44. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  45. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  46. }
  47. protected function setCreateTimeAttr($value)
  48. {
  49. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  50. }
  51. protected function setUpdateTimeAttr($value)
  52. {
  53. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  54. }
  55. }