abstract:一张简单的图书表: id name author delete_time ; 模型名为 Book模型:namespace app\index\model;use think\Model;use think\model\concern\SoftDelete;class Book extends Model{ use SoftDelete;
一张简单的图书表: id name author delete_time ; 模型名为 Book
模型:
namespace app\index\model;
use think\Model;
use think\model\concern\SoftDelete;
class Book extends Model
{
use SoftDelete;
protected $table = 'book'; //设置表名
protected $pk = 'id'; //设置主键
//软删除
protected $deleteTime = 'delete_time';
//字段默认值
protected $defaultSoftDelete = 0;
}
查询
public function sel_test(){
$res=Book::where('name', 'thinkphp')->find();
dump($res);
}
新增
public function add_test(){
$data=['name'=>'mysql','author'=>'xxx'];
$res=Book::create($data);
return $res->id;
}
修改
public function upd_test(){
第一种
$res= Book::get(1);
$res->name='test_name';
$res->save();
第二种
Book::update(['name'=>'test_name'],['id'=>1]);
}
删除:
物理删除:
Book::destroy(9,true); //单个
Book::destroy([6,7,8],true); //多个
软删除:
Book::destroy(9)
软删除需要在模型中 需要引入SoftDelete trait ,还要在模型中 use SoftDelete , 设置软删除字段,和默认值,表中也必须要有 软删除字段
Correcting teacher:天蓬老师Correction time:2019-04-01 10:08:24
Teacher's summary:像这样的 add_test() 的方法命名, 是不符合ThinkPHP的命名规范的, 建议编程前, 先把开发手册再读一下:
https://www.kancloud.cn/manual/thinkphp5_1/353949