亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

操作Users模型實(shí)現(xiàn)物理刪除與軟刪除;

Original 2019-04-21 16:09:41 177
abstract:<?php namespace app\index\controller; use think\Controller; use think\Db; use app\index\model\Users as UsersModel; class Users extends Controller { //
<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use app\index\model\Users as UsersModel;

class Users extends Controller
{
// 使用模型刪除  destroy();
public function del()
{
    // 使用閉包刪除
    UsersModel::destroy(function($query){
        $query->where('weight','>',130);// 體重大于130的刪除
    });
}


/**
 * 實(shí)現(xiàn)軟刪除的步驟:
 *  1、在數(shù)據(jù)庫表中(users) 添加字段:delete_time;
 *  2、在Users模型中引入SoftDelete類庫;think\model\concern\SoftDelete;
 *  3、在Users模型中添加兩個(gè)屬性
 *     $deleteTime = 'delete_time';// 與數(shù)據(jù)表字段名一致
 *     $defaultSoftDelete = 0;// 添加刪除標(biāo)志默認(rèn)值;
 *  4、在控制器中使用withTrashed() 方法實(shí)現(xiàn)軟刪除
 *  5、查看已刪除的軟刪除;onlyTrashed();
 *  6、恢復(fù)軟刪除 (new UsersModel)->restore(條件);
 */

// 使用模型實(shí)現(xiàn)軟刪除
public function softdelete()
{
    // 如果想在查詢的時(shí)候看到已經(jīng)被刪除的記錄  使用:withTrashed();
    $res = UsersModel::withTrashed()->where('uid','<',5)->select();
    dump($res);
}


// 恢復(fù)軟刪除 使用依賴注入方式恢復(fù)
public function restore(UsersModel $users)
{
    // 恢復(fù)軟刪除
    $res = $users->restore(['uid'=>1]);
    dump($res);
}
}

?>


Correcting teacher:天蓬老師Correction time:2019-04-22 10:19:30
Teacher's summary:如果你是軟刪除, 那么在查詢的時(shí)候, 一定要注意一下查詢條件... 是否允許過濾掉已軟刪除的記錄, ....

Release Notes

Popular Entries