Category.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace app\admin\model\ask;
  3. use think\Model;
  4. class Category extends Model
  5. {
  6. // 表名
  7. protected $name = 'ask_category';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. // 追加属性
  14. protected $append = [
  15. 'type_text',
  16. 'status_text'
  17. ];
  18. protected static function init()
  19. {
  20. self::afterInsert(function ($row) {
  21. $pk = $row->getPk();
  22. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  23. });
  24. }
  25. public function getTypeList()
  26. {
  27. return ['question' => __('Question'), 'article' => __('Article'), 'tag' => __('Tag'), 'expert' => __('Expert')];
  28. }
  29. public function getStatusList()
  30. {
  31. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  32. }
  33. public function getTypeTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  36. $list = $this->getTypeList();
  37. return isset($list[$value]) ? $list[$value] : '';
  38. }
  39. public function getStatusTextAttr($value, $data)
  40. {
  41. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  42. $list = $this->getStatusList();
  43. return isset($list[$value]) ? $list[$value] : '';
  44. }
  45. }