サマリー:一張簡(jiǎn)單的圖書(shū)表: id name author delete_time ; 模型名為 Book模型:namespace app\index\model;use think\Model;use think\model\concern\SoftDelete;class Book extends Model{ use SoftDelete;
一張簡(jiǎn)單的圖書(shū)表: 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'; //設(shè)置表名
protected $pk = 'id'; //設(shè)置主鍵
//軟刪除
protected $deleteTime = 'delete_time';
//字段默認(rèn)值
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); //單個(gè)
Book::destroy([6,7,8],true); //多個(gè)
軟刪除:
Book::destroy(9)
軟刪除需要在模型中 需要引入SoftDelete trait ,還要在模型中 use SoftDelete , 設(shè)置軟刪除字段,和默認(rèn)值,表中也必須要有 軟刪除字段
添削の先生:天蓬老師添削時(shí)間:2019-04-01 10:08:24
先生のまとめ:像這樣的 add_test() 的方法命名, 是不符合ThinkPHP的命名規(guī)范的, 建議編程前, 先把開(kāi)發(fā)手冊(cè)再讀一下:
https://www.kancloud.cn/manual/thinkphp5_1/353949