Comment.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace app\admin\model\shop;
  3. use think\Model;
  4. class Comment extends Model
  5. {
  6. // 表名
  7. protected $name = 'shop_comment';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'status_text'
  17. ];
  18. protected static function init()
  19. {
  20. self::afterWrite(function ($row) {
  21. $changedData = $row->getChangedData();
  22. if (isset($changedData['status']) && $row->goods) {
  23. if ($changedData['status'] == 'normal') {
  24. $row->goods->setInc('comments');
  25. } else {
  26. if ($row->goods->comments > 0) {
  27. $row->goods->setDec('comments');
  28. }
  29. }
  30. }
  31. });
  32. self::afterDelete(function ($row) {
  33. if ($row['pid']) {
  34. \addons\shop\model\Comment::where('id', $row['pid'])->setDec('comments');
  35. }
  36. });
  37. }
  38. public function getStatusList()
  39. {
  40. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  41. }
  42. public function getStatusTextAttr($value, $data)
  43. {
  44. $value = $value ?: ($data['status'] ?? '');
  45. $list = $this->getStatusList();
  46. return $list[$value] ?? '';
  47. }
  48. public function User()
  49. {
  50. return $this->hasOne('\\app\\common\\model\\User', 'id', 'user_id', [], 'LEFT');
  51. }
  52. public function Goods()
  53. {
  54. return $this->hasOne('Goods', 'id', 'goods_id', [], 'LEFT');
  55. }
  56. }