SearchKeyword.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class SearchKeyword extends Model
  5. {
  6. // 表名
  7. protected $table = 'search_keyword';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. // 追加属性
  14. protected $append = [
  15. 'sex_text'
  16. ];
  17. public function book()
  18. {
  19. return $this->belongsTo('book', 'book_id', 'id');
  20. }
  21. protected static function init()
  22. {
  23. self::afterInsert(function ($row) {
  24. $pk = $row->getPk();
  25. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  26. });
  27. }
  28. public function getSexList()
  29. {
  30. return ['1' => '男频','2' => '女频'];
  31. }
  32. public function getSexTextAttr($value, $data)
  33. {
  34. $value = $value ? $value : $data['sex'];
  35. $list = $this->getSexList();
  36. return isset($list[$value]) ? $list[$value] : '';
  37. }
  38. }