Product.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\admin\model\wwh;
  3. use think\Model;
  4. class Product extends Model
  5. {
  6. // 表名
  7. protected $name = 'wwh_product';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'tjdata_text'
  17. ];
  18. public function getTjdataList()
  19. {
  20. return ['0' => __('Tjdata 0'), '1' => __('Tjdata 1')];
  21. }
  22. public function getTjdataTextAttr($value, $data)
  23. {
  24. $value = $value ? $value : (isset($data['tjdata']) ? $data['tjdata'] : '');
  25. $list = $this->getTjdataList();
  26. return isset($list[$value]) ? $list[$value] : '';
  27. }
  28. public function productcategory()
  29. {
  30. return $this->hasone('\app\admin\model\wwh\Productcategory', 'id', 'productcategoryid')->setEagerlyType(0);
  31. }
  32. protected static function init()
  33. {
  34. self::beforeInsert(function ($row) {
  35. $pids = 0;
  36. if ($row->productcategoryid != 0) {
  37. $pids = self::getParentIds($row->productcategoryid);
  38. $pids .= "," . $row->productcategoryid;
  39. }
  40. $row->pids = $pids;
  41. });
  42. self::beforeUpdate(function ($row) {
  43. $changeData = $row->getChangedData();
  44. if (isset($changeData['productcategoryid'])) {
  45. $row->pids = self::getParentIds($row->productcategoryid) . ',' . $row->productcategoryid;
  46. }
  47. });
  48. }
  49. public static function getParentIds($pid) {
  50. $row = Productcategory::get($pid);
  51. if ( !$row ) {
  52. return 0;
  53. }
  54. return $row->pids;
  55. }
  56. }