使用教程
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);
}
}
}
~~~
- 序言及更新日志
- 前言一 開發PHP必備的環境(你可以不看)
- LinUX系統ThinkPHP5鏈接MsSQL數據庫的pdo_dblib擴展
- centos7.2掛載硬盤攻略
- Centos系統Redis安裝及Redis的PHP擴展安裝
- Centos系統增加Swap(系統交換區)的方法
- 前言二 開發PHP軟件配置和介紹(你依然可以不看)
- 數據庫SQL文件
- 本地Git(版本控制)的搭建
- GIT遠程倉庫的克隆和推送
- Git常用命令
- PHP面向對象思想實戰經驗領悟
- PHP面向對象實戰----命名空間
- PHP面向對象實戰----繼承
- 基類實戰--底層方法封裝
- 基類實戰--構造函數實戰
- 基類實戰--析構函數的使用
- TP5實戰開發前篇---控制器(controller)
- 控制器中Request類的使用
- 控制器中基類的使用
- TP5實戰開發前篇---模型篇(model)
- TP5實戰開發前篇---驗證器篇(Validate)
- TP5實戰課程入門篇---花拳繡腿
- 模塊以及類的文件的建立
- Api開發------單條信息顯示
- Api開發---單條信息復雜關聯顯示
- Api開發---查詢信息緩存Cache的應用
- TP5實戰技巧---開發思路 引路造橋
- TP5實戰技巧---整合基類 化繁為簡
- TP5實戰課程入門篇---數據操作
- Api開發---數據的添加和修改
- API開發---快速開發API通用接口
- TP5專用微信sdk使用教程
- THINKPHP5微信SDK更新記錄及升級指導
- TP5專用SDK 微信參數配置方法
- 微信公眾號推送接口對接教程
- 微信推送接口對接示例含掃描登錄微信端部分
- TP5專用微信支付SDK使用簡介
- TP5專用支付寶支付SDK使用說明
- 使用NW將開發的網站打包成桌面應用
- TP5高階實戰課程 進階篇概述
- 進階篇一 實戰開發之習慣及要求
- 進階篇二 實戰開發之控制器
- 控制器基類之控制器基類使用方法
- 控制器基類之控制器基類常用方法分享
- 控制器基類之構造函數的使用方法
- 進階篇三 實戰開發之權限控制
- TP5實戰源碼 --- 全局用戶信息驗證類Auth
- TP5實戰源碼 --- 微信Auth實戰開發源碼
- 進階篇四 實戰開發之模型
- 模型基類之模型基類的用途
- 模型基類之常用數據處理方法
- 模型邏輯層之實戰代碼(含事務)
- 模型實戰開發之模型常用方法
- 模型實戰源碼 --- 樂觀鎖的應用
- 模型實戰技巧---Model事件功能的使用
- 模型事件實戰應用---數據庫操作日志
- 進階篇五 實戰開發之緩存(Cache)
- TP5實戰源碼---應用緩存獲取城市信息
- TP5實戰源碼---應用緩存獲取分類詳情
- 進階篇六 TP5類庫的封裝和使用
- DataEdit快捷操作類庫
- ShowCode快捷使用類庫
- 阿里大于 短信API接口 TP5專用類庫
- DatabaseUpgrade數據庫對比及更新類庫
- AuthWeb權限類使用說明
- 進階篇七 服務層的應用
- 服務層源碼示例
- 服務層基類源碼
- 進階篇八 應用層Redis數據處理基類
- Redis服務層基類源碼
- 進階篇九 使用Redis類庫處理一般的搶購(秒殺)活動示例
- 進階篇十 某大型項目應用本Redis類源碼示例(含事務 樂觀鎖)
- 進階篇十一 邏輯層的應用
- 邏輯層基類源碼
- 進階篇 服務層代碼示例
- 高階實戰課程 進階篇持續新增中
- 高階篇一 TP5命令行之守護任務源碼
- TP5實戰源碼 --- 命令行
- TP5實戰源碼 --- 通過shell建立PHP守護程序
- 高階篇二 使用Redis隊列發送微信模版消息
- 高階篇二 之 Worker隊列基類源碼
- 高階篇三 TP5實戰之Redis緩存應用
- Redis實戰源碼之Hash專用類庫源碼
- Redis實戰源碼之Model類結合
- Redis實戰源碼之模型Hash基類源碼
- Redis實戰源碼之Hash查詢使用技巧
- Redis實戰源碼之 shell腳本中redis賦值和取值
- 高階篇四 Swoole的實戰應用
- swoole基類代碼
- Swoole擴展WebsocketServer專用類
- 基于Swoole的多Room聊天室的程序
- Swoole守護服務shell源碼
- 高階篇五 命令行異步多進程隊列類的應用
- tp_worker類源碼
- WorkerBase
- WorkerCommand
- WorkerRedis
- Redis類
- CycleWorkBase
- WorkerHookBase異步鉤子
- 隊列日志SQL
- 高階篇六 定時執行隊列類庫以及使用方法
- 定時隊列類庫源碼
- 高階篇七 異步執行循環隊列類庫以及使用教程
- CycleWorkBase源碼
- 高階實戰課程 進階篇持續新增中
- Extend便捷類庫源碼庫
- 阿里相關類庫
- SendSms--驗證碼API接口文件
- 權限相關類庫目錄
- AuthWeb 權限驗證類庫
- Redis便捷操作類庫(20171224更新)
- Redis
- Tools工具類庫集
- Curl類庫
- DataEdit
- Rand類庫
- ShowCode類庫
- Upload類庫
- 附件集合
- 附件一:微信支付 實戰開發源碼
- 微信支付類庫源代碼
- Common_util_pub.php
- DownloadBill_pub.php
- JsApi_pub.php
- NativeCall_pub.php
- NativeLink_pub.php
- OrderQuery_pub.php
- Refund_pub.php
- RefundQuery_pub.php
- SDKRuntimeException.php
- ShortUrl_pub.php
- UnifiedOrder_pub.php
- Wxpay_client_pub.php
- Wxpay_server_pub.php
- WxPayConf_pub.php
- 微信支付回調頁面源碼
- 附件二 順豐快遞BSP接口實戰開發源碼
- 順豐快遞BSP接口實戰開發源碼
- 順豐BSP基類
- 順豐BSP基礎代碼
- 順豐BSP下單接口
- 順豐BSP查單接口
- 順豐BSP確認/取消接口
- 附件三 APP注冊登陸接口源碼(含融云平臺接口)
- 附件四 TP5訂單Model(含事務 獲取器 修改器等方法)
- 附錄五 RSA加密解密
- Rsa文件源碼
- 附件六 阿里大于短信接口
- 附件七 AES加解密類
- AES加解密類源碼
- 附件八 TP5路由設置源碼
- 附件九 TP5 Excel導入導出下載便捷類庫
- Excel類庫TP5源碼
- 附件十 TP5便捷操作Redis類庫源碼
- TP5源碼 Redis操作便捷類庫
- 附件十一 TP5源碼 上傳文件入庫類源碼
- 上傳類Upload源碼
- Upload類上傳配置文件
- 存儲圖像文件的數據庫SQL文件
- 存儲文件的數據庫SQL文件
- 附件十二 TP5 圖片處理增強類 支持縮略圖在線顯示
- 附件十三 微信推送消息接口類庫源碼
- 附件十三 微信推送消息接口類庫源碼 之 基類
- 附件十四 存儲微信昵稱的處理方法