tp 模型Model 获取器
获取器的作用是在获取数据的字段值后自动进行处理,例如,我们需要对状态值进行转换
模型文件Team.php
namespace app\index\model; use \think\Model; use \think\Db; class Team extends Model { //带下划线的去掉下划线 如原属性dept_id public function getdeptIdAttr($value) { $dept=db('dept')->select(); $status=[]; foreach ($dept as $k => $v) { $status[$v['id']]=$v['name']; } // $status = [-1=>'删除',0=>'禁用',1=>'正常',2=>'待审核']; return $status[$value]; } }
控制器页Team.php
namespace app\index\controller; use \think\Controller; use think\Db; use app\index\model\Team as teamModel; class Team extends Controller { public function index() { $model = new teamModel();// 第一条数据 $first_id=db('team')->where('id > 0')->min('id'); $first=$model->where('id',$first_id)->find(); $this->assign("first",$first); return $this->fetch(); }