Product.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\admin\model\unishop;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. /**
  6. * 商品模型
  7. * Class Product
  8. * @package app\admin\model\unishop
  9. */
  10. class Product extends Model
  11. {
  12. use SoftDelete;
  13. //数据库
  14. protected $connection = 'database';
  15. // 表名
  16. protected $name = 'unishop_product';
  17. // 自动写入时间戳字段
  18. protected $autoWriteTimestamp = 'int';
  19. // 定义时间戳字段名
  20. protected $createTime = 'createtime';
  21. protected $updateTime = 'updatetime';
  22. protected $deleteTime = 'deletetime';
  23. // 追加属性
  24. protected $append = [
  25. 'activetime_text'
  26. ];
  27. protected static function init()
  28. {
  29. self::afterInsert(function ($row) {
  30. $pk = $row->getPk();
  31. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  32. });
  33. }
  34. public function getActivetimeTextAttr($value, $data)
  35. {
  36. $value = $value ? $value : (isset($data['activetime']) ? $data['activetime'] : '');
  37. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  38. }
  39. protected function setActivetimeAttr($value)
  40. {
  41. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  42. }
  43. /**
  44. * 关联分类
  45. * @return \think\model\relation\BelongsTo
  46. */
  47. public function category()
  48. {
  49. return $this->belongsTo('category');
  50. }
  51. /**
  52. * 关联运费
  53. * @return \think\model\relation\BelongsTo
  54. */
  55. public function delivery()
  56. {
  57. return $this->belongsTo('delivery');
  58. }
  59. }