abstract://model扩展类//model扩展类
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;
class User extends Model{
use SoftDelete;
protected $pk = 'id';
protected $table = 'reader';
protected $deleteTime = 'delete_time';
protected $defaultSoftDelete = 0;
}
//基于模型控制器方法 User.php
namespace app\index\controller;
use think\Controller;
use app\model\User as modelUser;
class User extends Controller{
//单条查询
public function sele(modelUser $user){
$res = $user::get(2);
dump($res);
}
//多条查询
public function select(modelUser $user){
$res = $user::all(function($query){
$query->where('salary','>','3000')
->where('age','>','32');
});
dump($res);
}
//插入单条数据
public function insert(modelUser $user){
$data = [
'name' => 'alice',
'age' => 28,
'tel' => '18956378989',
'email'=> '18956378989@139.com',
'salary'=> '5600'
];
$user::create($data);
}
//插入多条记录
public function inserAll(modelUser $user){
$data = [
['name' => 'alice','age' => 28,'tel' => '18956378989','email'=> '18956378989@139.com','salary'=> '5600'],
['name' => '陈大益','age' => 36,'tel' => '15986379696','email'=> '15986379696@139.com','salary'=> '3900'],
['name' => '许大上','age' => 42,'tel' => '15236897979','email'=> '15236897979@qq.com','salary'=> '6500']
];
$res = $user->insertAll($data);
echo $res ? '插入成功' : '插入失败';
}
//更新操作
public function update(modelUser $user){
$user::update(['salary' => \think\Db::raw('salary+500')],function($query){
$query->where('salary < 4000');
});
echo '更新成功';
}
//物理删除
public function delete(modelUser $user){
//$res = $user::destroy(4);
$res = $user::destroy(function($query){
$query->where('salary > 6000');
});
echo $res ? '删除成功' : '删除失败';
}
//软删除
public function softdel(modelUser $user){
$res = $user::destroy(6);
//普通查询看不到时间戳字段的记录
//dump($user::all());
//查询回收站的记录
//dump($user::onlyTrashed()->all());
//查询含回收站的全部记录
dump($user::withTrashed()->all());
}
}
Correcting teacher:天蓬老师Correction time:2019-11-01 14:40:00
Teacher's summary:在实际开发过程中, 任何用户数据都是重要, 对于不再需要的数据, 通过设置隐藏标志位来实现删除是很有用的