摘要:图书模型类<?php namespace app\index\model; use think\Model; use think\model\concern\SoftDelete; class Books extends Model { use SoftDelete; //数据表名称 protected
图书模型类
<?php
namespace app\index\model;
use think\Model;
use think\model\concern\SoftDelete;
class Books extends Model
{
use SoftDelete;
//数据表名称
protected $table = 'books';
//主键
protected $pk = 'id';
protected $deleteTime = 'delete_time';
/**
* 软删除字段默认值
* @var mixed
*/
protected $defaultSoftDelete = 0;
}控制器访问类
<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\Books;
class ModelTest extends controller
{
//新增操作
public function create()
{
$data = [
'name'=>'.NET编程',
'price'=>39.27
];
$books = Books::create($data);
echo '添加成功,id='.$books->id;
}
//查询单条记录
public function get()
{
$book = Books::get(function($query){
$query->where('price','<',50);
});
dump($book);
}
//查询多条记录
public function all()
{
$books = Books::all(function($query){
$query->where('price','>',50);
});
dump($books);
}
//更新
public function update()
{
Books::update([
'price' => \think\Db::raw('price+10')
],['id'=>5]);
echo '更新成功';
}
//物理删除
public function destroy()
{
Books::destroy(5);
echo '删除成功';
}
//软删除
public function soft_delete()
{
Books::destroy(1);
echo '删除成功';
}
//查询回收站的数据
public function getTrashed()
{
$books = Books::onlyTrashed()->all();
dump($books);
}
}
批改老师:天蓬老师批改时间:2019-01-22 09:13:30
老师总结:软删除是有一定条件, 本质上是基于更新与查询来实现的