[李景山php]每天TP5-20170114|thinkphp5-Model.php-7

來源:互聯網
上載者:User

標籤:thinkphp5

/** * 刪除記錄 * @access public * @param mixed $data 主鍵列表 支援閉包查詢條件 * @return integer 成功刪除的記錄數 */public static function destroy($data)// 刪除記錄{    $model = new static();// 擷取繼承者ID    $query = $model->db();// 擷取 資料庫 操作    if (is_array($data) && key($data) !== 0) {// 如果是數組 並且 key 不等於0        $query->where($data);// where 條件進行設定        $data = null;// 賦值為空白    } elseif ($data instanceof \Closure) {//如果是個匿名函數        call_user_func_array($data, [ & $query]);// 調用 $data 這樣的匿名函數,傳入 $query參數        $data = null;// 清空傳入的參數    } elseif (is_null($data)) {// 如果傳入參數 為空白        return 0;//直接返回0    }    $resultSet = $query->select($data);// 獲得結果集    $count     = 0;// 清空條目    if ($resultSet) {// 擷取結果集【對象】        foreach ($resultSet as $data) {// 遍曆 新函數            $result = $data->delete();// 刪除            $count += $result;// 累加        }    }    return $count;// 返回結果}/** * 命名範圍 * @access public * @param string|array|Closure  $name 命名範圍名稱 逗號分隔 * @param mixed                 ...$params 參數調用 * @return Model */public static function scope($name){    if ($name instanceof Query) {// 如果是查詢 執行個體化        return $name;    }    $model     = new static();// 執行個體自己 的 model    $params    = func_get_args();//擷取傳入的參數    $params[0] = $model->db();// 初始化 資料庫連接    if ($name instanceof \Closure) {//閉包        call_user_func_array($name, $params);//調用函數 傳遞參數    } elseif (is_string($name)) {// 字串        $name = explode(‘,‘, $name);    }    if (is_array($name)) {        foreach ($name as $scope) {// 多個函數調用            $method = ‘scope‘ . trim($scope);            if (method_exists($model, $method)) {                call_user_func_array([$model, $method], $params);// 類 函數 參數            }        }    }    return $model;// 返回 model}/** * 設定是否使用全域查詢範圍 * @param bool  $use 是否啟用全域查詢範圍 * @access public * @return Model */public static function useGlobalScope($use){    $model                  = new static();// 好處是 靜態    static::$useGlobalScope = $use;// 是否    return $model;// 返回設定}/** * 根據關聯條件查詢當前模型 * @access public * @param string    $relation 關聯方法名 * @param string    $operator 比較操作符 * @param integer   $count 個數 * @param string    $id 關聯表的統計欄位 * @return Model */public static function has($relation, $operator = ‘>=‘, $count = 1, $id = ‘*‘){    $model = new static();// 靜態執行個體化    $info  = $model->$relation()->getRelationInfo();// 擷取資訊    $table = $info[‘model‘]::getTable();// 表名    switch ($info[‘type‘]) {// 不同的類型        case Relation::HAS_MANY://產生普通的 join 語句            return $model->db()->alias(‘a‘)                ->join($table . ‘ b‘, ‘a.‘ . $info[‘localKey‘] . ‘=b.‘ . $info[‘foreignKey‘], $info[‘joinType‘])                ->group(‘b.‘ . $info[‘foreignKey‘])                ->having(‘count(‘ . $id . ‘)‘ . $operator . $count);        case Relation::HAS_MANY_THROUGH:// 居然還有未完成函數,牛叉            // TODO    }}/** * 根據關聯條件查詢當前模型 * @access public * @param string    $relation 關聯方法名 * @param mixed     $where 查詢條件(數組或者閉包) * @return Model */public static function hasWhere($relation, $where = []){    $model = new static();    $info  = $model->$relation()->getRelationInfo();// 關聯資訊    switch ($info[‘type‘]) {        case Relation::HAS_ONE:        case Relation::HAS_MANY:            $table = $info[‘model‘]::getTable();            if (is_array($where)) {                foreach ($where as $key => $val) {                    if (false === strpos($key, ‘.‘)) {                        $where[‘b.‘ . $key] = $val;                        unset($where[$key]);                    }                }            }            return $model->db()->alias(‘a‘)                ->field(‘a.*‘)                ->join($table . ‘ b‘, ‘a.‘ . $info[‘localKey‘] . ‘=b.‘ . $info[‘foreignKey‘], $info[‘joinType‘])                ->where($where);        case Relation::HAS_MANY_THROUGH:            // TODO    }}// 同上 產生使用 has where 的關聯查詢/** * 解析模型的完整命名空間 * @access public * @param string $model 模型名(或者完整類名) * @return string */protected function parseModel($model){    if (false === strpos($model, ‘\\‘)) {        $path = explode(‘\\‘, get_called_class());// 應該是有一些特殊的用法        array_pop($path);        array_push($path, Loader::parseName($model, 1));        $model = implode(‘\\‘, $path);    }// 擷取完整的 命名空間    return $model;}/** * 查詢當前模型的關聯資料 * @access public * @param string|array $relations 關聯名 * @return $this */public function relationQuery($relations){    if (is_string($relations)) {// 關係型查詢        $relations = explode(‘,‘, $relations);// 查詢 explode    }    $this->relation();// relation 關係    foreach ($relations as $relation) {// relations as        $this->data[$relation] = $this->relation->getRelation($relation);    }// 關係 匯入    return $this;}/** * 預載入關聯查詢 返回資料集 * @access public * @param array     $resultSet 資料集 * @param string    $relation 關聯名 * @return array */public function eagerlyResultSet($resultSet, $relation){    return $this->relation()->eagerlyResultSet($resultSet, $relation);}// 預載入關聯查詢 返回資料集合// 返回關聯 結果集


本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1881796

[李景山php]每天TP5-20170114|thinkphp5-Model.php-7

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.