-
推薦使用的方法
查看全部 -
修改器進行復雜加密
查看全部 -
模型修改器????set字段名Attr????
查看全部 -
獲取器的設置
查看全部 -
格式固定????get字段名Attr????獲取器的設置
查看全部 -
$res = Db:table('imooc_user')
-> where('id', '>', 10)
-> field('username, id')
-> order('id DESC')
// -> LIMIT(3, 5)
// -> page()
-> group() //order不生效
-> select();
dump($req);
查看全部 -
$db = Db::name('user');
$data = [];
$db->insert($data);
$db->insertGetId($data);
$Db->insertGetId($data);
查看全部 -
數(shù)據(jù)庫查詢的四個方法
查看全部 -
protected $auto = [ "time" // 字段名 ]; protected $insert = [ "time_insert"http:// 字段名 ]; protected $update = [ "time_update" ]; protected $AutoW設置自動更新時間 protected $update_time = "字段名"; protected $instpert_time = "字段名"; // 獲取設置值 public function get字段名Attr ($val) { return //根據(jù)不同條件返回不同的值 } // 新增設置值 public function set字段名Attr ($val) { return }查看全部
-
模型時間戳:參考筆記http://idcbgp.cn/notepad/20147e
軟刪除:參考筆記http://idcbgp.cn/notepad/2014a3查看全部 -
軟刪除:
首先在模型里面引用SoftDelete
<?php
namespace app\index\model;
use think\Model;
use traits\model\SoftDelete;
class User extends Model{
? ? use SoftDelete;
#刪除數(shù)據(jù)的時候刪除時間戳默認寫入字段delete_time中,當要自定義時:
// ? ?protected $deleteTime = '自定義刪除時間字段名';
}
然后在控制器里面執(zhí)行操作:
?public function index(){
// ? ? ? ?$res = User::destroy(4);//被軟刪除
// ? ? ? ?$res = User::get(4);//返回NULL
?
? ? ? ? #查詢包含已刪除的數(shù)據(jù)
? ? ? ? //$res = User::withTrashed(true)->find(2);
?
? ? ? ? #查詢僅包含已刪除的數(shù)據(jù)
? ? ? ? $res = User::onlyTrashed()->select();
? ? ? ? foreach ($res as $val){
? ? ? ? ? ? dump($val);
? ? ? ? }
? ? ? ? #若要恢復被軟刪除的數(shù)據(jù),直接用update方式將delete_time的值設置為NULL即可
?
? ? ? ? #當開啟軟刪除后要想真正徹底刪除數(shù)據(jù),在destroy的第二個參數(shù)后面?zhèn)魅胍粋€true值
? ? ? ? $res = User::destroy(1,true);
?
? ? ? ? #通過get方式進行軟刪除/刪除
? ? ? ? $res = User::get(3);//如果此處數(shù)據(jù)已經(jīng)被軟刪除則獲取到的為NULL,后面的操作無效
? ? ? ? $user->delete();//軟刪除
? ? ? ? $res = $user->delete(true);//刪除
? ? }
查看全部 -
thinkphp 提供的自動時間戳 自動記錄時間比使用自動完成功能好,減少代碼量;
1.修改database.php配置文件中設置'auto_timestamp' 為true (不推薦的方法,此法數(shù)據(jù)庫中所有表要有create_time和update_time這2個字段。)
2.(推薦)在模型的類里添加屬性:只對個別數(shù)據(jù)表字段create_time、update_time自動完成時間戳賦值,可以按如下操作:
protected $autoWriteTimestamp = true;?
//在模型中對約定的create_time、update_time自動完成時間戳賦值,如果不用默認的create_time名稱,這樣設置
protected $createTime = 'create_at';
protected $updateTime = 'update_at';
// 禁用某個時間戳,可以這樣設置
//protected $createTime = false; //表示這個創(chuàng)建自動記錄時間戳禁用
查看全部 -
圖2
auto (新增及更新的時候自動完成的屬性數(shù)組)
insert(僅新增的時候自動完成的屬性數(shù)組)
update(僅更新的時候自動完成的屬性數(shù)組)
查看全部 -
auto (新增及更新的時候自動完成的屬性數(shù)組)
insert(僅新增的時候自動完成的屬性數(shù)組)
update(僅更新的時候自動完成的屬性數(shù)組)
命名規(guī)范是 ?-> set + 屬性名的駝峰命名 ?+ Attr
格式: protected $insert = [
? ? ? ? 'insert'//字段名
? ? ? ? ? ? ? ? ? ? ? ? ? ?];
public function setInsertAttr(){
? ? ? ? return time();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
查看全部 -
?
#獲取器 get+字段名+Attr?
命名規(guī)范是? -> get + 屬性名的駝峰命名 + Attr?
#修改器 set+字段名+Attr?命名規(guī)范是? -> set + 屬性名的駝峰命名? + Attr
查看全部
舉報