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

                使用教程 http://www.hmoore.net/mikkle/thinkphp5_study/462704 ~~~ <?php /** * Created by PhpStorm. * Power By Mikkle * Email:776329498@qq.com * Date: 2017/11/27 * Time: 9:04 */ namespace mikkle\tp_tools; use mikkle\tp_master\Exception; use mikkle\tp_master\Request; use mikkle\tp_master\Log; use mikkle\tp_master\Loader; /** *$paramList = [ "company" => "company/s", "address" => "address/s", "contacts" => "contacts/s", "jobs" => "jobs/s", "mobile" => "mobile/s", "tencent_code" => "tencent_code/s", "desc" => "desc/s", "event_key" => "event_key/n", ]; $validate_name = "base/system/SystemApply"; $model_name = 'base/system/SystemApply'; $re = DataEdit::instance() ->setParameter($paramList) ->setAppend(["append" => "this is append"]) ->setValidate($validate_name) ->setModel($model_name) ->save();; return $re ? ReturnCode::jsonCode(1001) : ReturnCode::jsonCode(1003); * * Power: Mikkle * Email:776329498@qq.com * Class DataEdit * @package mikkle\tp_tools */ class DataEdit { protected $parameter; protected $append; protected $data; protected $saveData; protected $validate; protected $validateClass; protected $scene; protected $model; protected $modelType; protected $modelClass; protected $updateMap; protected $isUpdate; protected $allowField = true ; protected $pk; protected $request; protected $error; protected $result; protected static $instance; public function __construct($options=[]) { $this->request=Request::instance(); if(isset($options["data"])){ $this->data=$options["data"]; } if(isset($options["validate"])){ $this->validate=$options["validate"]; } if(isset($options["model"])){ $this->model=$options["model"]; } } /** * 靜態初始化方法 * Power: Mikkle * Email:776329498@qq.com * @param array $options * @return static */ public static function instance($options = []) { if (is_null(self::$instance)) { self::$instance = new static($options); } return self::$instance; } /** * 設置獲取參數數組 * Power: Mikkle * Email:776329498@qq.com * @param array $parameter * @return $this * @throws Exception */ public function setParameter(array $parameter=[]){ switch (true){ case (!empty($parameter)&&is_array($parameter)): $this->parameter=$parameter; return $this; break; default: throw new Exception("設置獲取參數數組的值出錯!"); } } /** * 單個添加獲取參數數組 * Power: Mikkle * Email:776329498@qq.com * @param $saveName * @param string $inputName * @param string $dataType * @return $this * @throws Exception */ public function addParameter($saveName,$inputName="",$dataType=""){ if (!is_string($saveName)||empty($saveName)){ throw new Exception("設置參數的類型必須為字符串"); } if (empty($inputName)){ $inputName=$saveName; } switch ($dataType) { case "string": case "s": $dataType = "/s"; break; case "int": case "d": $dataType = "/d"; break; case "array": case "a": $dataType = "/a"; break; case "bool": case "b": $dataType = "/b"; break; case "float": case "f": $dataType = "/f"; break; default: $dataType = ""; } $this->parameter["$saveName"]="{$inputName}{$dataType}"; return $this; } /** * 設置追加的參數 * Power: Mikkle * Email:776329498@qq.com * @param array $append * @return $this * @throws Exception */ public function setAppend(array $append=[]){ switch (true){ case (!empty($append)&&is_array($append)): $this->append=$append; return $this; break; default: throw new Exception("設置獲取參數數組的值出錯!"); } } /** * 添加單個附加數據 * Power: Mikkle * Email:776329498@qq.com * @param $saveName * @param $saveData * @return $this * @throws Exception */ public function addAppendData($saveName,$saveData){ if (!is_string($saveName)||!is_string($saveData)){ throw new Exception("設置附加存儲值必須使用字符串"); } $this->append[$saveName]=$saveData; return $this; } /** * 設置保存的值 * Power: Mikkle * Email:776329498@qq.com * @param array $data * @return $this * @throws Exception */ public function setData($data=[]){ switch (true){ case (!empty($data)&&is_array($data)): $this->data=$data; return $this; break; default: throw new Exception("設置的值出錯!"); } } /** * 設置驗證器或者驗證規則[array] * Power: Mikkle * Email:776329498@qq.com * @param array $validate * @return $this * @throws Exception */ public function setValidate($validate=[]){ switch (true){ case ($validate===false): return $this; break; case (empty($validate)): throw new Exception("設置的值不能為空!"); break; case (!is_string($validate)&&!is_array($validate)): throw new Exception("設置值的類型必須是字符串!"); break; default: $this->validate=$validate; return $this; } } /** * 設置model * Power: Mikkle * Email:776329498@qq.com * @param string $model * @return $this * @throws Exception */ public function setModel($model=""){ switch (true){ case (empty($model)): throw new Exception("設置的值不能為空!"); break; case (!is_string($model)): throw new Exception("設置值的類型必須是字符串!"); break; default: $this->model=$model; return $this; } } public function setModelType($modelType=""){ switch (true){ case (empty($modelType)): throw new Exception("設置的值不能為空!"); break; case (!is_string($modelType)): throw new Exception("設置值的類型必須是字符串!"); break; default: $this->modelType=$modelType; return $this; } } /** * 設置pk * @title setPk * @description * @author Mikkle * @url * @param $pk * @return $this */ public function setPk($pk){ $this->pk=$pk; return $this; } /** * 設置升級條件 * Power: Mikkle * Email:776329498@qq.com * @param $map * @return $this */ public function setUpdateMap($map){ $this->updateMap=$map; $this->isUpdate=true; return $this; } /** * 設置可以允許的字段 * Power: Mikkle * Email:776329498@qq.com * @param $allowField * @return $this */ public function setAllowField($allowField){ $this->allowField=$allowField; return $this; } /** * 存儲數據 數據存在自動判斷添加和升級 * Power: Mikkle * Email:776329498@qq.com * @return bool */ public function save(){ try{ if (!$this->actionHandle()){ throw new Exception($this->getError()); } $this->checkPk(); if ($this->updateMap && isset($this->saveData[$this->pk])){ if (!isset($this->updateMap[$this->pk] )){ unset($this->saveData[$this->pk]); } } if(empty($this->updateMap)&&isset($this->saveData[$this->pk])){ $this->updateMap[$this->pk] = $this->saveData[$this->pk]; unset($this->saveData[$this->pk]); } if ($this->updateMap){ $this->result = $this->modelClass->allowField($this->allowField)->save($this->saveData,$this->updateMap); }else{ $this->result = $this->modelClass->allowField($this->allowField)->save($this->saveData); } return $this->result; }catch (Exception $e){ $this->error=$e->getMessage(); Log::error($e->getMessage()); return false; } } /** * 升級數據 * Power: Mikkle * Email:776329498@qq.com * @return bool */ public function update(){ try{ if (!$this->actionHandle()){ throw new Exception($this->getError()); } $this->checkPk(); if ($this->updateMap && isset($this->saveData[$this->pk])){ if (!isset($this->updateMap[$this->pk] )){ unset($this->saveData[$this->pk]); } } if (empty($this->updateMap)&&!isset($this->saveData[$this->pk])){ throw new Exception("升級條件缺失"); }else if(empty($this->updateMap)&&isset($this->saveData[$this->pk])){ $this->updateMap[$this->pk] = $this->saveData[$this->pk]; unset($this->saveData[$this->pk]); } if ($this->updateMap){ $this->result = $this->modelClass->allowField($this->allowField)->save($this->saveData,$this->updateMap); return $this->result; }else{ throw new Exception("升級條件不存在"); } }catch (Exception $e){ $this->error=$e->getMessage(); Log::error($e->getMessage()); return false; } } /** * 強制為添加數據(會過濾掉PK字段) * Power: Mikkle * Email:776329498@qq.com * @return bool */ public function add(){ try{ if (!$this->actionHandle()){ throw new Exception($this->getError()); } $this->checkPk(); if ($this->isUpdate&&isset($this->saveData[$this->pk])){ unset($this->saveData[$this->pk]); $this->isUpdate=false; } $this->result = $this->modelClass->allowField($this->allowField)->save($this->saveData); return $this->result; }catch (Exception $e){ $this->error=$e->getMessage(); Log::error($e->getMessage()); return false; } } /** * 執行model方法 * Power: Mikkle * Email:776329498@qq.com * @param $actionName * @return bool */ public function execModelAction($actionName){ try{ if (!$this->actionHandle()){ throw new Exception($this->getError()); } if (!method_exists($this->modelClass, $actionName)) { throw new Exception("自定義的方法存在"); } $this->result = $this->modelClass->$actionName($this->saveData); return $this->result; }catch (Exception $e){ $this->error=$e->getMessage(); Log::error($e->getMessage()); return false; } } protected function actionHandle() { try{ $this->buildSaveData(); if (!$this->checkSaveDate()){ return false; } if (empty($this->modelType)){ $this->modelClass=Loader::model($this->model); }else{ $this->modelClass=Loader::model($this->model,$this->modelType); } return true; }catch (Exception $e){ Log::error($e->getMessage()); return false; } } protected function checkPk(){ if (empty($this->pk)){ $this->pk= $this->modelClass->getPK(); } if (isset($this->saveData[$this->pk])){ $this->isUpdate=true; } } protected function checkSaveDate(){ switch (true) { case (empty($this->saveData)): $this->error="要保存的數據為空"; return false; break; case (empty($this->validate)): return true; break; case (is_array($this->validate)): $this->validateClass = Loader::validate(); $this->validateClass->rule($this->validate); break; default: if (strpos($this->validate, '.')) { // 支持場景 list($this->validate, $this->scene) = explode('.', $this->validate); } $this->validateClass = Loader::validate($this->validate); if (!empty($scene)) { $this->validateClass->scene($scene); } } if (!$this->validateClass->check($this->saveData)) { $this->error=$this->validateClass->getError(); return false; } else { return true; } } protected function buildSaveData(){ switch (true) { case (empty($this->parameter)&&empty($this->data)): $this->saveData=$this->request->param(); break; case (!empty($this->parameter)&&!empty($this->data)): throw new Exception("參數和指定值不可同時設置,容許指定請使用append方法"); break; case ($this->parameter): $this->saveData = $this->buildParameter($this->parameter); break; case ($this->data): $this->saveData = $this->data; break; default: ; } if ($this->append) { $this->saveData=array_merge($this->saveData,$this->append); } } /** * 獲取參數的方法 * Power: Mikkle * Email:776329498@qq.com * @param $array * @return array */ static public function buildParameter($array) { $data=[]; $request=Request::instance(); foreach( $array as $item=>$value ){ $data[$item] = $request->param($value); } return $data; } /** * 獲取model * Power: Mikkle * Email:776329498@qq.com * @return mixed */ public function getModel(){ return $this->modelClass; } /** * 獲取錯誤信息 * Power: Mikkle * Email:776329498@qq.com * @return mixed|string */ public function getError(){ switch (true) { case (empty($this->error)): return ""; break; case (is_string($this->error)): return $this->error; break; case (is_array($this->error)&&isset($this->error["msg"])): return $this->error["msg"]; break; case (is_array($this->error)): return json_encode($this->error); break; default: return json_encode($this->error); } } } ~~~
                  <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>

                              哎呀哎呀视频在线观看