Zone.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace app\admin\model\ask;
  3. use think\Model;
  4. class Zone extends Model
  5. {
  6. // 表名
  7. protected $name = 'ask_zone';
  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. $refreshTags = function ($row) {
  21. $tags = input('post.tags');
  22. Tag::where('zone_id', $row['id'])->update(['zone_id' => 0]);
  23. Tag::where('id', 'in', $tags)->update(['zone_id' => $row['id']]);
  24. };
  25. self::afterInsert(function ($row) {
  26. $pk = $row->getPk();
  27. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  28. });
  29. self::beforeUpdate(function ($row) {
  30. $row['updatetime'] = time();
  31. });
  32. self::afterInsert($refreshTags);
  33. self::afterUpdate($refreshTags);
  34. }
  35. public function getTagsAttr($value, $data)
  36. {
  37. $tags = Tag::where('zone_id', $data['id'])->column('id');
  38. return implode(',', $tags);
  39. }
  40. public function getStatusList()
  41. {
  42. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  43. }
  44. public function getStatusTextAttr($value, $data)
  45. {
  46. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  47. $list = $this->getStatusList();
  48. return isset($list[$value]) ? $list[$value] : '';
  49. }
  50. }