亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

快速入門ThinkPHP 5.0 --模型篇

難度中級
時長 3小時 0分
學習人數
綜合評分9.67
70人評價 查看評價
10.0 內容實用
9.5 簡潔易懂
9.5 邏輯清晰
  • 使用助手函數model()獲取模型
    查看全部
    0 采集 收起 來源:什么是模型

    2018-05-12

  • 使用loader獲取模型
    查看全部
    0 采集 收起 來源:什么是模型

    2018-05-12

  • 使用model
    查看全部
    0 采集 收起 來源:什么是模型

    2018-05-12

  • 鏈式操作
    查看全部
    0 采集 收起 來源:鏈式操作

    2018-05-12

  • where寫法
    查看全部
    0 采集 收起 來源:條件構造器

    2018-05-11

  • where寫法
    查看全部
    0 采集 收起 來源:條件構造器

    2018-05-11

  • 輸出SQL語句
    查看全部
    0 采集 收起 來源:條件構造器

    2018-05-11

  • 更新數據
    查看全部
  • 插入數據
    查看全部
  • 查詢2
    查看全部
  • 查詢
    查看全部
  • 軟刪除:

    首先在模型里面引用SoftDelete
    <?php
    namespace?app\index\model;
    use?think\Model;
    use?traits\model\SoftDelete;
    class?User?extends?Model{
    ????use?SoftDelete;
    ????#autowritetimestamp表示創建與更新的時間戳都被打開
    ????protected?$autoWriteTimestamp?=?true;
    ????#刪除數據的時候刪除時間戳默認寫入字段delete_time中,當要自定義時:
    //????protected?$deleteTime?=?'自定義刪除時間字段名';
    }

    然后在控制器里面執行操作

    <?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
    
    ????????#查詢包含已刪除的數據
    ????????//$res?=?User::withTrashed(true)->find(2);
    
    ????????#查詢僅包含已刪除的數據
    ????????$res?=?User::onlyTrashed()->select();
    ????????foreach?($res?as?$val){
    ????????????dump($val);
    ????????}
    ????????#若要恢復被軟刪除的數據,直接用update方式將delete_time的值設置為NULL即可
    
    ????????#當開啟軟刪除后要想真正徹底刪除數據,在destroy的第二個參數后面傳入一個true值
    ????????$res?=?User::destroy(1,true);
    
    ????????#通過get方式進行軟刪除/刪除
    ????????$res?=?User::get(3);//如果此處數據已經被軟刪除則獲取到的為NULL,后面的操作無效
    ????????$user->delete();//軟刪除
    ????????$res?=?$user->delete(true);//刪除
    ????}
    }


    查看全部
  • 模型時間戳:

    <?php
    namespace?app\index\model;
    use?think\Model;
    
    class?User?extends?Model{
    ????#autowritetimestamp表示創建與更新的時間戳都被打開
    ????protected?$autoWriteTimestamp?=?true;
    //????#createtime為false表示創建的時間戳被關閉
    //????protected?$createTime?=?false;
    //????#updatetime為true表示更新時間戳被打開
    //????protected?$updateTime?=?true;
    //????#創建數據的時候創建時間戳默認寫入字段create_time中,當要自定義時:
    //????protected?$createTime?=?'自定義創建時間字段名';
    //????#更新數據的時候更新時間戳默認寫入字段update_time中,當要自定義時:
    //????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'];
    //????}
    
    ????#無論對數據庫執行插入&更新操作,總是在數據中加上time字段相應的值
    ????protected?$auto?=?[
    ??????'time'//字段名
    ????];
    ????public?function?setTimeAttr(){
    ????????return?time();//字段對應的值
    ????}
    
    ????#insert/update對數據庫執行插入/更新操作時,在數據字段中加入對應的值
    ????protected?$insert?=?[
    ????????'insert'//字段名
    ????];
    ????public?function?setInsertAttr(){
    ????????return?time();
    ????}
    }


    查看全部
  • 模型獲取器:

    首先設置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);//獲取性別單欄數據
    ????????dump($res->toArray());//以數組形式獲取整條數據,性別顯示為男/女/未知
    ????????dump($res->getData());//以原始形式獲取整條數據,性別顯示為1/2/其他
    ????}
    }


    查看全部
    0 采集 收起 來源:模型獲取器

    2018-05-08

舉報

0/150
提交
取消
課程須知
1、有一定的php基礎。 2、對ThinkPHP5 有一定的了解。 3、沒有接觸過tp5 的學生可以先看下我之前錄制的 <快速入門 ThinkPHP5 基礎篇>
老師告訴你能學到什么?
1、ThinkPHP5 的數據庫操作類 2、ThinkPHP5 的數據模型 3、使用數據模型快速對數據庫進行增刪改查的操作 4、ThinkPHP5 的自動完成操作 5、模型的自動時間戳和軟刪除

微信掃碼,參與3人拼團

微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

友情提示:

您好,此課程屬于遷移課程,您已購買該課程,無需重復購買,感謝您對慕課網的支持!