批改状态:未批改
老师批语:
<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\Staff as StaffModel;
class Index extends Controller{
// 闭包查询
public function select(){
$staff = StaffModel::all(function($query){
$query -> where('age','<',50)->where('salary','>',3000);
});
dump($staff);
}
// 软删除
/**
* 软删除需要现在model 模型中进行定义软删除
* 在数据库表中添加软删除字段,此指端在数据库中存储以linux时间戳的方式进行存储
* 在软删除中采用用的方式为更新方式,在需要删除的数据的删除字段中更新时间戳
* 在一般的查询中是无法查询到软删除的数据。
* 需要采用
* User::withTrashed()->find();
User::withTrashed()->select();
* 的方式进行查询,
* 软删除回复,相同,也是采用更新操作,
* 将准备回复的数据进行更新操作,将时间戳字段的数据作为null。
* 一般查询方可查询。
*/
public function softDelete(){
//软删除
// StaffModel::destroy(7);
//恢复被软删除的数据
$res = StaffModel::onlyTrashed() -> find(7);
$res -> restore();
}
}点击 "运行实例" 按钮查看在线实例
模型
<?php
namespace app\index\model;
use think\Model;
// 导入think自带软删除功能
use think\model\concern\SoftDelete;
class Staff extends Model{
use SoftDelete;
// 设置表名
protected $table = 'staff';
protected $id = 'id';
// 设置删除时间段字段
protected $deleteTime = 'delete_time';
//设置默认值
protected $defaultSoftDelete = 0;
}点击 "运行实例" 按钮查看在线实例
软删除需要现在model 模型中进行定义软删除
* 在数据库表中添加软删除字段,此指端在数据库中存储以linux时间戳的方式进行存储
* 在软删除中采用用的方式为更新方式,在需要删除的数据的删除字段中更新时间戳
* 在一般的查询中是无法查询到软删除的数据。
* 需要采用
* User::withTrashed()->find();
User::withTrashed()->select();
* 的方式进行查询,
* 软删除回复,相同,也是采用更新操作,
* 将准备回复的数据进行更新操作,将时间戳字段的数据作为null。
* 一般查询方可查询。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号