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'; //設(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); //單個
Book::destroy([6,7,8],true); //多個
軟刪除:
Book::destroy(9)
軟刪除需要在模型中 需要引入SoftDelete trait ,還要在模型中 use SoftDelete , 設(shè)置軟刪除字段,和默認(rèn)值,表中也必須要有 軟刪除字段
Correcting teacher:天蓬老師Correction time:2019-04-01 10:08:24
Teacher's summary:像這樣的 add_test() 的方法命名, 是不符合ThinkPHP的命名規(guī)范的, 建議編程前, 先把開發(fā)手冊再讀一下:
https://www.kancloud.cn/manual/thinkphp5_1/353949