Jishuguifan.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. class Jishuguifan extends Model
  6. {
  7. // 表名
  8. protected $table = 'jishuguifan';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'integer';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'ismenu_text'
  18. ];
  19. protected static function init()
  20. {
  21. //设置顶级tid
  22. self::afterInsert(function ($row){
  23. $tid = Db::name('jishuguifan')->where('id',$row['pid'])->value('tid');
  24. Db::name('jishuguifan')->where('id',$row['id'])->update(['tid'=>$tid]);
  25. });
  26. self::beforeUpdate(function ($row) {
  27. $tid = Db::name('jishuguifan')->where('id',$row['pid'])->value('tid');
  28. Db::name('jishuguifan')->where('id',$row['id'])->update(['tid'=>$tid]);
  29. });
  30. }
  31. public function getIsmenuList()
  32. {
  33. return ['1' => __('Ismenu 1'), '0' => __('Ismenu 0')];
  34. }
  35. public function getIsmenuTextAttr($value, $data)
  36. {
  37. $value = $value ? $value : (isset($data['ismenu']) ? $data['ismenu'] : '');
  38. $list = $this->getIsmenuList();
  39. return isset($list[$value]) ? $list[$value] : '';
  40. }
  41. public function company()
  42. {
  43. return $this->belongsTo('Company', 'company_id', 'id', [], 'LEFT')->setEagerlyType(0);
  44. }
  45. }