<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## 1 新模型解讀 TP5RC3中重點更新了Model的功能。 新的Model是**TP5設計目標——為API開發而設計**的核心功能。 新的Model與以前版本的功能完全不同,下面進行分析。 ## 2 新模型源代碼 ### 2-1 Model類的聲明 ~~~ ;聲明Model為抽象類并實現序列化接口和數組訪問接口功能 abstract class Model implements \JsonSerializable, \ArrayAccess {} ~~~ ### 2-2 成員變量 ~~~ ;數據庫模型數組,數據庫連接配置,模型操作的表,回調事件 private static $links = []; protected static $connection = []; protected static $table; protected static $event = []; ;模型操作表的主鍵,數據表操作的錯誤信息,自定義模型名稱,字段驗證規則 protected $pk; protected $error; protected $name; protected $validate; ;字段屬性信息數組,待操作數據數組,緩存數據數組,修改字段數組 protected $field = []; protected $data = []; protected $cache = []; protected $change = []; ;自動完成數組,新增自動完成數組,更新自動完成數組,自動寫入時間戳數組,時間戳默認格式 protected $auto = []; protected $insert = []; protected $update = []; protected $autoTimeField = ['create_time', 'update_time', 'delete_time']; protected $dateFormat = 'Y-m-d H:i:s'; ;字段類型或格式轉換數組,是否為更新操作,模型對應關聯對象,當前模型的父模型,模型實例數組 protected $type = []; protected $isUpdate = false; protected $relation; protected $parent; protected static $initialized = []; ~~~ ### 2-3 public成員方法 #### 1 __construct() 創建模型實例 ~~~ public function __construct($data = []) ~~~ > $data 模型的數據參數 ~~~ ;創建對象傳入$data參數 $user = new User(['name'=>'thinkphp','email'=>'thinkphp@qq.com']); $user->save(); ;實例化模型,設置$this->data['name']為'thinkphp',調用save()更新數據 $user = new User; $user->name= 'thinkphp'; $user->save(); ~~~ #### 2 data() 設置數據對象值 ~~~ public function data($data = '') ~~~ > $data 操作數據參數 ~~~ ;設置模型的$data $user = new User; $user->data(['name'=>'thinkphp','email'=>'thinkphp@qq.com']); $user->save(); ~~~ #### 3 getData() 獲取對象原始數據 ~~~ public function getData($name = '') ~~~ > $name 數據的鍵名 在定義了getXXAttr()時使用獲取原始數據 #### 4 toArray() 將數據轉換為數組 ~~~ public function toArray() ~~~ #### 5 getPk() 獲取模型的主鍵信息 ~~~ public function getPk($table = '') ~~~ #### 6 save() 更新數據到數據庫 ~~~ public function save($data = [], $where = [], $getInsertId = true) ~~~ > $data 插入的數據 > $where 更新條件 > $getInsertId 是否獲取自增主鍵 ~~~ ;傳入數據更新 $user = new User; $user->save(['name' => 'thinkphp'],['id' => 1]); ;使用模型數據更新 $user = User::get(1); $user->name = 'thinkphp'; $user->save(); ;使用閉包實現復雜條件 $user = new User; $user->save(['name' => 'thinkphp'],function($query){ // 更新status值為1 并且id大于10的數據 $query->where('status',1')->where('id','>',10); }); ~~~ #### 7 saveAll() 修改多個數據到數據庫 ~~~ public function saveAll($dataSet) ~~~ > $dataSet 待插入數據集合 #### 8 allowField() 設置運行更新的字段 ~~~ public function allowField($field) ~~~ #### 9 isUpdate() 檢查當前操作是否為更新操作 ~~~ public function isUpdate($update = true) ~~~ #### 10 delete() 對數據進行刪除處理 ~~~ public function delete() ~~~ ~~~ ;刪除模型數據 $user = User::get(1); $user->delete(); ~~~ #### 11 auto() 設置自動完成字段,在autoCompleteData中檢查 ~~~ public function auto($fields) ~~~ ~~~ class User extends Model{ protected $auto = [ 'update_time']; protected $insert = [ 'create_time','name','status'=>1 ]; protected $update = ['name']; protected function setNameAttr($value){ return strtolower($value); } } $user = new User; $user->name = 'ThinkPHP'; $user->save(); echo $user->name; // thinkphp echo $user->create_time; // 14601053899 ~~~ #### 12 validate() 設置自動驗證規則 ~~~ public function validate($rule = true, $msg = []) ~~~ > $rule 驗證規則 > $msg 提示信息 使用見 驗證類分析 #### 13 validateData() 對當前data進行驗證處理 ~~~ public function validateData() ~~~ #### 14 getError() 獲取操作錯誤信息 ~~~ public function getError() ~~~ #### 15 event() 注冊事件回調接口 ~~~ public static function event($event, $callback, $override = false) ~~~ >$event 事件名稱 > 支持的事件類型,在模型操作過程中會進行回調before_delete、after_delete、before_write、after_write、before_update、after_update、before_insert、after_insert > $callback 事件回調接口 ~~~ ;注冊before_insert事件, User::event('before_insert',function($user){ if($user->status != 1){ return false; } }); ~~~ #### 16 create() 寫入數據 ~~~ public static function create($data = []) ~~~ > $data 待寫入數據 ~~~ User::create(['name'=>'thinkphp','email'=>'thinkphp@qq.com']); ~~~ #### 17 update() 更新數據 ~~~ public static function update($data = [], $where = []) ~~~ > $data 待更新數據 > $where 更新條件 ~~~ User::where('id',1)->update(['name' => 'thinkphp']); ~~~ #### 18 get() 數據查詢 ~~~ public static function get($data = '', $with = [], $cache = false) ~~~ ~~~ $user = User::get(1); echo $user->name; // 使用閉包查詢 $user = User::get(function($query){ $query->where('name','thinkphp'); }); echo $user->name; ~~~ #### 19 all() 查詢多個記錄 ~~~ public static function all($data = [], $with = [], $cache = false) ~~~ ~~~ // 根據主鍵獲取多個數據 $list = User::all('1,2,3'); // 或者使用數組 $list = User::all([1,2,3]); foreach($list as $key=>$user){ echo $user->name; } // 使用閉包查詢 $list = User::all(function($query){ $query->where('status',1)->limit(3)->order('id','asc'); }); foreach($list as $key=>$user){ echo $user->name; } ~~~ #### 20 destroy() 刪除記錄 ~~~ public static function destroy($data) ~~~ ~~~ User::destroy(1); // 支持批量刪除多個數據 User::destroy('1,2,3'); // 或者 User::destroy([1,2,3]); ;閉包刪除 User::destroy(function($query){ $query->where('id','>',10); }); ~~~ #### 21 scope() 設置命名范圍 ~~~ public static function scope($name, $params = []) ~~~ ~~~ class User extends Model{ public function scopeThinkphp($query){ $query->where('name','thinkphp')->field('id,name'); } public function scopeAge($query){ $query->where('age','>',20)->limit(10); } } // 查找name為thinkphp的用戶 User::scope('thinkphp')->get(); // 查找年齡大于20的10個用戶 User::scope('age')->all(); // 查找name為thinkphp的用戶并且年齡大于20的10個用戶 User::scope('thinkphp,age')->all(); User::scope(function($query){ $query->where('age','>',20)->limit(10); })->all(); $user = new User; // 查找name為thinkphp的用戶 $user->thinkphp()->get(); // 查找年齡大于20的10個用戶 $user->age()->all(); // 查找name為thinkphp的用戶并且年齡大于20的10個用戶 $user->thinkphp()->age()->all(); ~~~ #### 22 has() hasWhere() 在關聯中進行查找 ~~~ public static function has($relation, $operator = '>=', $count = 1, $id = '*') public static function hasWhere($relation, $where = []) ~~~ #### 23 relationQuery() 執行關聯查詢 ~~~ public function relationQuery($relations) ~~~ #### 24 getRelationInfo() 獲取關聯信息 ~~~ public function getRelationInfo($name = '') ~~~ #### 25 eagerlyResultSet() eagerlyResult()預載入關聯查詢 ~~~ public function eagerlyResultSet($resultSet, $relation) public function eagerlyResult($result, $relation) ~~~ #### 26 hasOne() belongsTo() hasMany() belongsToMany() 關聯定義 ~~~ ;一對一關聯 public function hasOne($model, $foreignKey = '', $localKey = '') ;相對關聯 public function belongsTo($model, $foreignKey = '', $otherKey = '') ;一對多關聯 public function hasMany($model, $foreignKey = '', $localKey = '') ;多對多關聯 public function belongsToMany($model, $table = '', $localKey = '', $foreignKey = '') ~~~ ~~~ ;一對一關聯 class User extends Model { public function profile() { return $this->hasOne('Profile'); } } $user = User::find(1); // 輸出Profile關聯模型的email屬性 echo $user->profile->email; ;自定義關聯外鍵 namespace app\index\model; use think\Model; class User extends Model { public function profile() { return $this->hasOne('Profile','uid'); } } ;關聯查詢 // 查詢有檔案記錄的用戶 $user = User::has('profile')->find(); // 查詢年齡是20歲的用戶 $user = User::hasWhere('profile',['age'=>'20'])->select(); ;關聯新增 $user = User::find(1); // 如果還沒有關聯數據 則進行新增 $user->profile->email = 'thinkphp'; $user->profile->save(); // 或者 $user->profile->save(['email' => 'thinkphp']); ;關聯更新 $user = User::find(1); $user->profile->email = 'thinkphp'; $user->profile->save(); // 或者 $user->profile->save(['email' => 'thinkphp']); ~~~ ~~~ ;相對關聯定義 class Profile extends Model { public function user() { return $this->belongsTo('User'); } } 相對關聯查詢 $profile = Profile::find(1); // 輸出User關聯模型的屬性 echo $profile->user->account; ~~~ ~~~ ;一對多關聯定義 class Article extends Model { public function comments() { return $this->hasMany('Comment'); } } ;一對多自定義主鍵 class Article extends Model { public function comments() { return $this->hasMany('Comment','art_id'); } } ;關聯查詢 $article = Article::find(1); // 獲取文章的所有評論 dump($article->comments); // 也可以進行條件搜索 dump($article->comments()->where('status',1)->select()); // 查詢評論超過3個的文章 $list = Article::has('comments','>',3)->select(); // 查詢評論狀態正常的文章 $list = Article::hasWhere('comments',['status'=>1])->select(); ;關聯新增 $article = Article::find(1); // 增加一個關聯數據 $article->comments()->save(['content'=>'test']); // 批量增加關聯數據 $article->comments()->saveAll([ ['content'=>'thinkphp'], ['content'=>'onethink'], ]); ~~~ ~~~ ;多對多關聯 class User extends Model { public function roles() { return $this->belongsToMany('Role'); } } ;關聯查詢 $user = User::get(1); // 獲取用戶的所有角色 dump($user->roles); // 也可以進行條件搜索 dump($user->roles()->where('name','admin')->select()); ;關聯新增 $user = User::get(1); // 增加關聯數據 會自動寫入中間表數據 $user->roles()->save(['name'=>'管理員']); // 批量增加關聯數據 $user->roles()->saveAll([ ['name'=>'管理員'], ['name'=>'操作員'], ]); ;單獨新增中間表 $user = User::get(1); // 僅增加關聯的中間表數據 $user->roles()->save(1); // 或者 $role = Role::get(1); $user->roles()->save($role); // 批量增加關聯數據 $user->roles()->saveAll([1,2,3]); ;單獨更新中間表 $user = User::get(1); // 增加關聯的中間表數據 $user->roles()->attach(1); // 傳入中間表的額外屬性 $user->roles()->attach(1,['remark'=>'test']); // 刪除中間表數據 $user->roles()->detach([1,2,3]); ~~~ #### 27 db() 初始化模型的數據庫對象 ~~~ public static function db() ~~~ #### 28 __call() __callStatic() __set() __get() __isset() __unset() __toString() __wakeup()攔截方法 ~~~ ;實例對象調用支持 public function __call($method, $args) ;類靜態調用支持 public static function __callStatic($method, $params) ;修改數據$this->data public function __set($name, $value) ;獲取數據$this->data public function __get($name) ;檢查數據是否存在 public function __isset($name) ;刪除數據 public function __unset($name) ;字符串輸出 public function __toString() ;解序列化后處理 public function __wakeup() ~~~ #### 29 jsonSerialize() 序列化接口實現 ~~~ public function jsonSerialize() ~~~ > json序列化輸出數據$data ~~~ echo json_encode(User::find(1)); {"id":"1","name":"","title":"","status":"1","update_time":"1430409600","score":"90.5"} ~~~ #### 30 offsetSet() offsetExists() offsetUnset() offsetGet()數組訪問支持 ~~~ public function offsetSet($name, $value) public function offsetExists($name) public function offsetUnset($name) public function offsetGet($name) ~~~ ~~~ $user = User::find(1); echo $user->name ; // 有效 echo $user['name'] // 同樣有效 $user->name = 'thinkphp'; // 有效 $user['name'] = 'thinkphp'; // 同樣有效 $user->save(); ~~~ ### 2-4protected成員方法 ~~~ protected function initialize() ;模型初始化接口 protected static function init() ;模型初始化回調 protected function isPk($key) ;判斷$key是否為主鍵 protected function autoCompleteData($auto = []) ;處理字段更新字段列表 protected function trigger($event, &$params) ;觸發注冊的事件回調 protected static function parseQuery(&$data, $with, $cache) ;分析查詢表達式 protected function parseModel($model) ;解析模型的命名空間 ~~~ ## 3 新模型總結
                  <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>

                              哎呀哎呀视频在线观看