<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] ## 統一示例 ``` use think\Model; use traits\model\SoftDelete; class User extends Model{ // 軟刪除 use SoftDelete; //需要引入SoftDelete trait protected $deleteTime = 'delete_time'; // 時間戳寫入 protected $autoWriteTimestamp = true; // 只讀字段 protected $readonly = ['name', 'email']; //類型轉 protected $type = [ 'status' => 'integer', 'score' => 'float', 'birthday' => 'timestamp:Y/m/d', 'info' => 'array', ]; //數據完成 protected $auto = []; protected $insert = ['ip', 'status' => 1]; // ip 回調用 setIpAttr 方法 protected $update = ['login_ip']; protected function setIpAttr(){ return request()->ip(); } // 修改器 public function setNameAttr($value){ return strtolower($value); } //獲取器,可獲取不存在字段 public function getStatusAttr($value){ $status = [-1 => '刪除', 0 => '禁用', 1 => '正常', 2 => '待審核']; return $status[$value]; } // 定義全局的查詢范圍 protected function base($query){ $query->where('status', 1); } protected function scopeThinkphp($query){ $query->where('name', 'thinkphp')->field('id,name'); } protected function scopeAge($query){ $query->where('age', '>', 20)->limit(10); } } ``` ## 技巧 ### 自定義不存在的字段 ``` //$data 為該條記錄數組 public function getLoanRateAttr($value,$data){ $loan = LoanModel::get($data['id']); $value = "參考".$loan['time_type']."利率: ".strval($value)."%"; return $value; } ``` ### 查詢某條記錄的某個值 ``` //使用 ->value $loanTypeId = LoanTypeModel::where(['name' => $loanTypeId])->value('id'); //返回 字符串 3 //如果是colunm 則是對多條記錄返回值 ``` ### 帶條件分頁 ``` $loanData = LoanModel::where($where)->paginate(10,false,[ 'query'=>$this->request->param(), ]); ``` ### 插入前檢查字段名 ``` fields_strict' => true, //檢查字段名 //異常可以用異常捕獲 try{ OrderModel::create($saveData); } catch(\Exception $e){ show($e->getMessage(),10004); } ``` ### 追加關聯數組(緊接在數組后) ``` //只是針對 單條記錄 $order = OrderModel::get(1)->appendRelationAttr('loan', 'max_money,min_money'); ``` ### 獲取器 獲取器的作用是在獲取數據的字段值后自動進行處理 ``` class User extends Model { public function getStatusAttr($value) { $status = [-1=>'刪除',0=>'禁用',1=>'正常',2=>'待審核']; return $status[$value]; } } // 使用 $user = User::get(1); echo $user->status; // 例如輸出“正常” ``` 獲取器還可以定義數據表中不存在的字段 ``` class User extends Model { public function getStatusTextAttr($value,$data) { $status = [-1=>'刪除',0=>'禁用',1=>'正常',2=>'待審核']; return $status[$data['status']]; } } // 使用 $user = User::get(1); echo $user->status_text; // 例如輸出“正常” ``` #### 獲取原始數據 ``` $user = User::get(1); // 獲取原始字段數據 echo $user->getData('status'); // 獲取全部原始數據 dump($user->getData()); ``` ### 修改器 修改器的作用是可以在數據賦值的時候自動進行轉換處理 ``` class User extends Model { public function setNameAttr($value) { return strtolower($value); } } // 使用 $user = new User(); $user->name = 'THINKPHP'; $user->save(); echo $user->name; // thinkphp ``` ### 時間戳 ``` // 全局開啟自動寫入時間戳字段 'auto_timestamp' => true, // 在模型中開啟 protected $autoWriteTimestamp = true; ``` 如果這兩個地方設置為true,默認識別為整型int類型,如果你的時間字段不是int類型的話,例如使用datetime類型的話,可以這樣設置: ``` // 開啟自動寫入時間戳字段 'auto_timestamp' => 'datetime', //or protected $autoWriteTimestamp = 'datetime'; ``` 字段名默認創建時間字段為create_time,更新時間字段為update_time demo ``` $user = new User(); $user->name = 'THINKPHP'; $user->save(); echo $user->create_time; // 輸出類似 2016-10-12 14:20:10 echo $user->update_time; // 輸出類似 2016-10-12 14:20:10 ``` ### 自定義時間戳 ``` class User extends Model { // 定義時間戳字段名 protected $createTime = 'create_at'; protected $updateTime = 'update_at'; } ``` ### 只讀字段 只讀字段 ``` namespace app\index\model; use think\Model; class User extends Model { protected $readonly = ['name','email']; } ``` ### 軟刪除 對數據頻繁使用刪除操作會導致性能問題,軟刪除的作用就是把數據加上刪除標記,而不是真正的刪除,同時也便于需要的時候進行數據的恢復 ``` namespace app\index\model; use think\Model; use traits\model\SoftDelete; class User extends Model { use SoftDelete; //需要引入SoftDelete trait protected $deleteTime = 'delete_time'; } ``` 使用 ``` // 軟刪除 User::destroy(1); // 真實刪除 User::destroy(1,true); $user = User::get(1); // 軟刪除 $user->delete(); // 真實刪除 $user->delete(true); ``` ### 類型轉換 會在**寫入和讀取**的時候自動進行類型轉換處理 ``` class User extends Model { protected $type = [ 'status' => 'integer', 'score' => 'float', 'birthday' => 'datetime', 'info' => 'array', ]; } // 使用 $user = new User; $user->status = '1'; $user->score = '90.50'; $user->birthday = '2015/5/1'; $user->info = ['a'=>1,'b'=>2]; $user->save(); var_dump($user->status); // int 1 var_dump($user->score); // float 90.5; var_dump($user->birthday); // string '2015-05-01 00:00:00' var_dump($user->info);// array (size=2) 'a' => int 1 'b' => int 2 ``` #### timestamp / datetime timestamp 寫入時為時間戳,讀取時默認為默認的格式為`Y-m-d H:i:s` 自定義時間格式 ``` class User extends Model { protected $type = [ 'status' => 'integer', 'score' => 'float', 'birthday' => 'timestamp:Y/m/d', ]; } ``` datetime 寫入和讀取數據的時候都會自動處理成時間字符串`Y-m-d H:i:s`的格式 ### 數據完成 系統支持`auto`、`insert`和`update`三個屬性,`auto`包含`insert`和`update` namespace app\index\model; ``` namespace app\index\model; use think\Model; class User extends Model { protected $auto = []; protected $insert = ['ip','status' => 1]; // ip 回調用 setIpAttr 方法 protected $update = ['login_ip']; protected function setIpAttr() { return request()->ip(); } } ``` ### 查詢范圍 > [參考](http://www.hmoore.net/manual/thinkphp5/138865) ``` namespace app\index\model; use think\Model; class User extends Model { protected function scopeThinkphp($query) { $query->where('name','thinkphp')->field('id,name'); } protected function scopeAge($query) { $query->where('age','>',20)->limit(10); } } // 使用 // 查找name為thinkphp的用戶 User::scope('thinkphp')->find(); // 查找年齡大于20的10個用戶 User::scope('age')->select(); // 查找name為thinkphp的用戶并且年齡大于20的10個用戶 User::scope('thinkphp,age')->select(); ``` #### 參數支持 ``` namespace app\index\model; use think\Model; class User extends Model { protected function scopeAgeAbove($query, $lowest_age) { $query->where('age','>',$lowest_age)->limit(10); } } User::scope('ageAbove', 20)->select(); ``` #### 全局查詢范圍 ``` namespace app\index\model; use think\Model; class User extends Model { // 定義全局的查詢范圍 protected function base($query) { $query->where('status',1); } } // 使用 $user = User::get(1); 最終的查詢條件會是 status = 1 AND id = 1 // 關閉全局查找 User::useGlobalScope(false)->get(1); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看