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'; //【自定義的數據庫字段】

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM