-
使用助手函數(shù)model()獲取模型查看全部
-
使用loader獲取模型查看全部
-
使用model查看全部
-
鏈?zhǔn)讲僮?查看全部
-
where寫法查看全部
-
where寫法查看全部
-
輸出SQL語句查看全部
-
更新數(shù)據(jù)查看全部
-
插入數(shù)據(jù)查看全部
-
查詢2查看全部
-
查詢查看全部
-
軟刪除:
首先在模型里面引用SoftDelete<?php namespace?app\index\model; use?think\Model; use?traits\model\SoftDelete; class?User?extends?Model{ ????use?SoftDelete; ????#autowritetimestamp表示創(chuàng)建與更新的時間戳都被打開 ????protected?$autoWriteTimestamp?=?true; ????#刪除數(shù)據(jù)的時候刪除時間戳默認(rèn)寫入字段delete_time中,當(dāng)要自定義時: //????protected?$deleteTime?=?'自定義刪除時間字段名'; }
然后在控制器里面執(zhí)行操作
<?php namespace?app\index\controller; use?think\Controller; use?app\index\model\User; class?Index?extends?Controller { ????public?function?index(){ //????????$res?=?User::destroy(4);//被軟刪除 //????????$res?=?User::get(2);//返回NULL ????????#查詢包含已刪除的數(shù)據(jù) ????????//$res?=?User::withTrashed(true)->find(2); ????????#查詢僅包含已刪除的數(shù)據(jù) ????????$res?=?User::onlyTrashed()->select(); ????????foreach?($res?as?$val){ ????????????dump($val); ????????} ????????#若要恢復(fù)被軟刪除的數(shù)據(jù),直接用update方式將delete_time的值設(shè)置為NULL即可 ????????#當(dāng)開啟軟刪除后要想真正徹底刪除數(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);//刪除 ????} }
查看全部 -
模型時間戳:
<?php namespace?app\index\model; use?think\Model; class?User?extends?Model{ ????#autowritetimestamp表示創(chuàng)建與更新的時間戳都被打開 ????protected?$autoWriteTimestamp?=?true; //????#createtime為false表示創(chuàng)建的時間戳被關(guān)閉 //????protected?$createTime?=?false; //????#updatetime為true表示更新時間戳被打開 //????protected?$updateTime?=?true; //????#創(chuàng)建數(shù)據(jù)的時候創(chuàng)建時間戳默認(rèn)寫入字段create_time中,當(dāng)要自定義時: //????protected?$createTime?=?'自定義創(chuàng)建時間字段名'; //????#更新數(shù)據(jù)的時候更新時間戳默認(rèn)寫入字段update_time中,當(dāng)要自定義時: //????protected?$updateTime?=?'自定義更新時間字段名'; }
查看全部 -
模型修改器與自動完成
<?php namespace?app\index\model; use?think\Model; class?User?extends?Model{ ????#get+字段名+Attr ????public?function?getGenderAttr($val){ ????????switch?($val){ ????????????case?"1"; ????????????????return?'男'; ?????????????????break; ????????????case?"2"; ?????????????????return?'女'; ??????????????????break; ????????????default; ?????????????????return?'未知'; ?????????????????break; ????????} ????} ????#模型修改 //????public?function?setPasswordAttr($val,$data){ //????????return?$val.$data['email']; //????} ????#無論對數(shù)據(jù)庫執(zhí)行插入&更新操作,總是在數(shù)據(jù)中加上time字段相應(yīng)的值 ????protected?$auto?=?[ ??????'time'//字段名 ????]; ????public?function?setTimeAttr(){ ????????return?time();//字段對應(yīng)的值 ????} ????#insert/update對數(shù)據(jù)庫執(zhí)行插入/更新操作時,在數(shù)據(jù)字段中加入對應(yīng)的值 ????protected?$insert?=?[ ????????'insert'//字段名 ????]; ????public?function?setInsertAttr(){ ????????return?time(); ????} }
查看全部 -
模型獲取器:
首先設(shè)置User模型:
<?php namespace?app\index\model; use?think\Model; class?User?extends?Model{ ????#get+字段名+Attr ????public?function?getGenderAttr($val){ ????????switch?($val){ ????????????case?"1"; ????????????????return?'男'; ?????????????????break; ????????????case?"2"; ?????????????????return?'女'; ??????????????????break; ????????????default; ?????????????????return?'未知'; ?????????????????break; ????????} ????} }
然后在index控制器中操作:
<?php namespace?app\index\controller; use?think\Controller; use?app\index\model\User; class?Index?extends?Controller { ????public?function?index(){ ????????$res?=?User::get(1); ????????dump($res->gender);//獲取性別單欄數(shù)據(jù) ????????dump($res->toArray());//以數(shù)組形式獲取整條數(shù)據(jù),性別顯示為男/女/未知 ????????dump($res->getData());//以原始形式獲取整條數(shù)據(jù),性別顯示為1/2/其他 ????} }
查看全部
舉報
0/150
提交
取消