数据库软删除php,ThinkPHP_5的模型时间戳和软删除

【模型时间戳】

应用配置文件【不过一般不这样用】

// 自动写入时间戳字段

‘auto_timestamp‘ => true,

【控制器块】

namespace app\index\controller;

use think\Controller;

use app\index\model\App;

class Index extends Controller

{

public function index()

{

$res = App::create([

‘name‘=>‘kail‘,

‘is_encryption‘=>1,

]);

dump($res);

}

}

【Model块】

class App extends Model

{

use softDelete;//【数据库字段为detele_time】

//默认字段是 create_time update_time

protected $autoWriteTimestamp = true;

//如果字段不是默认定义的字段[create_time update_time],要定义

// protected $createTime = ‘create_at‘; //【自定义的数据库字段】

// protected $updateTime = ‘update_at‘; //【自定义的数据库字段】

}

【软删除】

【控制器块】

public function index()

{

//$res = App::destroy(20);

//$res = App::get(20); //软删除后获取不到数据,为NULL

//要想获取到包含软删除数据

//$res = App::withTrashed(true)->find(20);

//dump($res->getData());//获取原始的数据

//想要获取软删除的全部数据

// $res = App::onlyTrashed()->select();

// foreach($res as $val){

// dump($val->getData());

// }

//开启软删除后想要真正的删除

//$res = App::destroy(20,true);//【方式一】

$app = App::get(21);

$res = $app->delete(true);//【方式二】

dump($res);

}

【Model块】

namespace app\index\model;

use think\Model;

use traits\model\SoftDelete;//【使用软删除】

class App extends Model

{

use SoftDelete;//【数据库字段为detele_time】

//默认字段是 create_time update_time

protected $autoWriteTimestamp = true;

//如果字段不是默认定义的字段[create_time update_time],要定义

// protected $createTime = ‘create_at‘; //【自定义的数据库字段】

// protected $updateTime = ‘update_at‘; //【自定义的数据库字段】

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值