PHP day8 TP5 model更新,软删除

1.  模型时间戳

     一般不开启database配置文件    里的'aotu_timestamp =true'

   

    而可以在model里单独开启,可以如下开启时间戳的功能

 

public class User{
    protected $autoWriteTimestamp = true;
}

   注意

protected $autoWriteTimestamp = true;

 

    开启后,默认会对数据表  中 create_time 和 update_time 字段自动完成

 

如果数据表中的字段名 不是create_time 和 update_time 则需要 申明他们的属性

也可以关闭 单独某字段的自动完成

 

public class User{
    protected $autoWriteTimestamp = true;

    //新增数据的时间戳字段,默认为create_time
    protected $createTime = 'create_tm';
    //更新数据的时间戳字段,默认为update_time
    //protected $updateTime = 'update_tm';
    //关闭updateTime  
    protected $updateTime = false;
}

 

 

2. 软删除  

在数据表中建 (默认) delete_time 字段,默认为null,  记录被删除的时间戳

<?php
use traits\model\SoftDelete
public class User{
       use SoftDelete;   //开启了软删除
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_tm';
}

 

若 想更改默认的delete_time 字段

在model中使用:

  protected $deleteTime = 'delete_tm';

 

 

3.  获取(包含被软删除的数据)  使用  withTrashed(true)

例如

$res = User::withTrashed(true)->find(2);

 

4.  仅获取被删除的数据

$res = User::onlyTrashed()->select();//返回数组

 

5.  恢复被软删除的数据

     直接 update  delete_time 字段

 

=============================

 

6.  如果想物理删除数据

使用

$res = User::destroy(2,true);

或者

$user = User::get(2);
$res = $user->delete(true);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值