ThinkPHP_5的模型時間戳記和虛刪除

來源:互聯網
上載者:User

標籤:des   資料庫   name   time   think   ext   bsp   func   xtend   

【模型時間戳記】

應用設定檔【不過一般不這樣用】
// 自動寫入時間戳記欄位
‘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‘; //【自訂的資料庫欄位】

}

ThinkPHP_5的模型時間戳記和虛刪除

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.