User.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\admin\model\ask;
  3. use think\Db;
  4. use think\Model;
  5. class User extends Model
  6. {
  7. // 表名
  8. protected $name = 'ask_user';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = false;
  11. // 定义时间戳字段名
  12. protected $createTime = false;
  13. protected $updateTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'flag_text'
  17. ];
  18. public static function init()
  19. {
  20. self::afterWrite(function ($row) {
  21. $changedData = $row->getChangedData();
  22. if (isset($changedData['experttitle'])) {
  23. Db::name("user")->where('id', $row['user_id'])->update(['title' => $row['isexpert'] ? $row['experttitle'] : '']);
  24. }
  25. });
  26. }
  27. public function getFlagList()
  28. {
  29. return ['index' => __('Index'), 'recommend' => __('Recommend'), 'new' => __('New')];
  30. }
  31. public function getFlagTextAttr($value, $data)
  32. {
  33. $value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
  34. $valueArr = explode(',', $value);
  35. $list = $this->getFlagList();
  36. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  37. }
  38. protected function setFlagAttr($value)
  39. {
  40. return is_array($value) ? implode(',', $value) : $value;
  41. }
  42. public function basic()
  43. {
  44. return $this->belongsTo("\app\common\model\User", 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  45. }
  46. public function category()
  47. {
  48. return $this->belongsTo("Category", 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
  49. }
  50. }