<?php
namespace app\index\model;
use think\Model;
use think\model\concern\SoftDelete;
class User extends Model
{
protected $table = 'user';
protected $pk = 'user_id';
use softDelete;
protected $deleteTime = 'delete_time';
protected $defaultSoftDelete = NULL;
//获取器一般用法
protected function getStatusAttr($value)
{
$status = [1=>'启用',0=>'禁用'];
return $status[$value];
}
//获取器第二种用法,传入第二个参数,引用全部的字段值
protected function getMoneyAttr($value, $data)
{
return $data['name'].'的工资是'.($value+5);
}
//可以位表中不存在的字段设置
protected function getStaffInfoAttr($value, $data)
{
//staff_info:是虚拟字段
//$value:仅仅是一个占位符,没有意义
return '我是'.$data['name'].",工资才".$data['money'];
}
//修改器
protected function setEntryTimeAttr($value){
return strtotime($value);
}
//修改器2,支持第二个参数,引用全部字段值
protected function setMoneyAttr($value, $data)
{
return $value+$data['age'];
}
protected function setIpAttr()
{
return request()->ip();
}
//类型转换,我们读取数据库的数据的时候默认的是字符串类型
protected $type = [
'entry_time' =>'timestamp:Y-m-d',
];
//模型的自动完成,针对写操作的,新增及更新
//相当于给字段设置默认值
protected $insert = [
'age' => 18,
'money' =>2000,
];
protected $update = [
'age' =>11
];
protected $auto = [
'sex' =>1,
'ip'
];
//开启当前模型的自动时间戳
protected $autoWriteTimestamp = true;
//设置一下用户自定义的新增和更新时间的字段名
protected $createTime = "create_time";
protected $updateTime = "update_time";
}
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!