<?php
namespace app\index\model;
use think\Model;
class Product extends Model{
public function getStatusAttr($value)
{
$status = [-1=>‘删除‘,0=>‘禁用‘,1=>‘正常‘,2=>‘待审核‘];
return $status[$value];
//获取器获取数据的字段值后自动进行处理,通过获取器定义,自动转换为字符串描述
}
public function getStatusTextAttr($value,$data)
{
$status = [-1=>‘删除‘,0=>‘禁用‘,1=>‘正常‘,2=>‘待审核‘];
return $status[$data[‘status‘]];
//获取器定义数据表中不存在的字段
}
public function setNameAttr($value)
{
return strtolower($value);
}
protected $createTime = ‘create_at‘;
protected $updateTime = ‘update_at‘;
// 定义时间戳字段名
}
?>
模型调用
$product = new ModelProduct();
$product->data = ([
‘uname‘=>‘asd‘,
‘admin‘=>‘asd‘,
‘gold‘=>123456
]);
$product->save();
echo $product->id;
//获取自增ID
$list = [
[‘uname‘=>‘thinkphp‘,‘gold‘=>200],
[‘uname‘=>‘onethink‘,‘gold‘=>300]];
$product->saveAll($list);
$user = ModelProduct::get(1);
$user->uname = ‘thinkphp1‘;
$user->gold = 300;
$user->save();
//查找到ID=1的行,然后对其更新数据
$user = new ModelProduct();// save方法第二个参数为更新条件
$user->save([
‘uname‘ => ‘thinkphp2‘,
‘gold‘ => 999]
,[‘id‘ => 1]);
//直接根据ID的筛选条件确定行再更新数据
$user = ModelProduct::get(1);
echo $user->name;
//查询单个数据
$user = ModelProduct::get(2);
$user->delete();*/
//删除模型数据,可以在实例化后调用delete方法
$info = model(‘Product‘)->where(‘id‘,3)->find();
echo $info->status;
$user = new Model();
$user->name = ‘THINKPHP‘;
$user->save();
echo $user->name;