標籤: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的模型時間戳記和虛刪除