zoukankan      html  css  js  c++  java
  • ThinkPHP_5的模型时间戳和软删除

    【模型时间戳】

    应用配置文件【不过一般不这样用】
    // 自动写入时间戳字段
    'auto_timestamp' => true,


    【控制器块】
    namespace appindexcontroller;

    use thinkController;
    use appindexmodelApp;

    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 appindexmodel;

    use thinkModel;
    use traitsmodelSoftDelete;//【使用软删除】

    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'; //【自定义的数据库字段】

    }

  • 相关阅读:
    系统程序员成长计划并发(二)(下)
    Web开发必知的八种隔离级别
    国产Android视频,Broncho A1
    Android中的BatteryService及相关组件
    Struts2输出XML格式的Result
    系统程序员成长计划并发(三)(上)
    入选”第一期中国最受欢迎50大技术博客”
    Broncho团队招聘应届毕业生(包括大四学生) 2名
    系统程序员成长计划并发(三)(下)
    WordPress MailUp插件‘Ajax’函数安全绕过漏洞
  • 原文地址:https://www.cnblogs.com/Caveolae/p/7152040.html
Copyright © 2011-2022 走看看