From 14abde51f48118675667d64fc8618a8365f2613a Mon Sep 17 00:00:00 2001 From: chengwei Date: Wed, 29 Nov 2023 12:29:12 +0800 Subject: [PATCH 1/3] =?UTF-8?q?add:=201=E3=80=81=E7=A6=81=E7=94=A8DB=20aft?= =?UTF-8?q?er=20event=20=E5=9B=9E=E8=B0=83=EF=BC=8C=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E6=AD=BB=E5=BE=AA=E7=8E=AF=E4=BA=8B=E4=BB=B6=E4=BA=A7=E7=94=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/model/RelationModel.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/think/model/RelationModel.php b/src/think/model/RelationModel.php index 888f1a5..905ad3a 100644 --- a/src/think/model/RelationModel.php +++ b/src/think/model/RelationModel.php @@ -125,6 +125,11 @@ public function getRelationTableName($relation) */ protected function _after_find(&$result, $options) { + if(!empty($options['disable_db_after']) && $options['disable_db_after'] === true){ + // 禁用db后处理事件 + return; + } + // 获取关联数据 并附加到结果中 if (!empty($options['link'])) { $this->getRelation($result, $options['link']); @@ -138,6 +143,11 @@ protected function _after_find(&$result, $options) */ protected function _after_select(&$result, $options) { + if(!empty($options['disable_db_after']) && $options['disable_db_after'] === true){ + // 禁用db后处理事件 + return; + } + // 获取关联数据 并附加到结果中 if (!empty($options['link'])) { $this->getRelations($result, $options['link']); @@ -154,6 +164,11 @@ protected function _after_select(&$result, $options) */ protected function _after_insert($pk, $pkName, $data, $options) { + if(!empty($options['disable_db_after']) && $options['disable_db_after'] === true){ + // 禁用db后处理事件 + return; + } + //写入事件日志 if ($options["model"] != "EventLog") { $this->databaseEventLogHook([ @@ -183,6 +198,11 @@ protected function _after_insert($pk, $pkName, $data, $options) */ protected function _after_update($result, $pkName, $data, $options, $writeEvent) { + if(!empty($options['disable_db_after']) && $options['disable_db_after'] === true){ + // 禁用db后处理事件 + return; + } + //写入事件日志 if ($result > 0 && $options["model"] != "EventLog" && $writeEvent) { $this->databaseEventLogHook([ @@ -212,6 +232,11 @@ protected function _after_update($result, $pkName, $data, $options, $writeEvent) */ protected function _after_delete($result, $pkName, $data, $options) { + if(!empty($options['disable_db_after']) && $options['disable_db_after'] === true){ + // 禁用db后处理事件 + return; + } + //写入事件日志 if ($result > 0 && $options["model"] != "EventLog") { $this->databaseEventLogHook([ From 98087bd9bb0a1ac5927e54f3649777d9a812b391 Mon Sep 17 00:00:00 2001 From: chengwei Date: Wed, 29 Nov 2023 12:29:59 +0800 Subject: [PATCH 2/3] =?UTF-8?q?add:=20=E6=94=AF=E6=8C=81update=20json->ref?= =?UTF-8?q?erence=5Fasset=20=E5=B1=80=E9=83=A8=E6=9B=B4=E6=96=B0json?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/Model.php | 21 ++++++++++++++++++--- src/think/db/Driver.php | 17 ++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/think/Model.php b/src/think/Model.php index 0d611ed..0c29e3c 100644 --- a/src/think/Model.php +++ b/src/think/Model.php @@ -769,10 +769,16 @@ protected function _before_update($pk, &$data, $options, $writeEvent) if ($options["model"] != "EventLog" && $writeEvent) { $oldData = $this->where($options["where"])->find(); foreach ($data as $key => $value) { - if ($oldData[$key] != $value) { + if (false !== strpos($key, '->')) { + // json数据更新兼容取值,不精确到具体的值 + [$indexKey, $name] = explode('->', $key, 2); + }else{ + $indexKey = $key; + } + if ($oldData[$indexKey] != $value) { //仅记录变化字段 - $this->oldUpdateData[$key] = $oldData[$key]; - $this->newUpdateData[$key] = $value; + $this->oldUpdateData[$indexKey] = $oldData[$indexKey]; + $this->newUpdateData[$indexKey] = $value; } } $this->oldUpdateKey = $oldData[$pk]; @@ -2765,6 +2771,15 @@ public function hint($hintContent) return $this; } + /** + * 禁用DB after event 回调,防止死循环事件产生 + * @return $this + */ + public function disableDBAfter(){ + $this->options['disable_db_after'] = true; + return $this; + } + /** * 获取执行的SQL语句 * @access public diff --git a/src/think/db/Driver.php b/src/think/db/Driver.php index 0462440..c1e9688 100644 --- a/src/think/db/Driver.php +++ b/src/think/db/Driver.php @@ -566,7 +566,22 @@ protected function parseSet($data) { $set = []; foreach ($data as $key => $val) { - if (isset($val[0]) && 'exp' == $val[0]) { + + if (false !== strpos($key, '->')) { + // 处理局部json字段数据更新 + [$key, $name] = explode('->', $key, 2); + $item = $this->parseKey($key); + + if(!empty($val) &&is_array($val)){ + // json数据格式保持原样不转义 + $val = json_encode($val); + $jsonSql = 'JSON_SET(' . $item . ', \'$.' . $name . '\', CAST(\'' . $val . '\' AS JSON))'; + }else{ + $jsonSql = 'JSON_SET(' . $item . ', \'$.' . $name . '\', \'' . $val . '\')'; + } + + $set[] = $item.'='.$jsonSql; + }elseif (isset($val[0]) && 'exp' == $val[0]) { $set[] = $this->parseKey($key) . '=' . $val[1]; } elseif (is_null($val)) { $set[] = $this->parseKey($key) . '=NULL'; From 63ee0819a7916aa50b5440dfe2122266f39d49c4 Mon Sep 17 00:00:00 2001 From: chengwei Date: Wed, 29 Nov 2023 12:32:06 +0800 Subject: [PATCH 3/3] =?UTF-8?q?add:=20parseDsn=20=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/db/Driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/think/db/Driver.php b/src/think/db/Driver.php index c1e9688..440cd90 100644 --- a/src/think/db/Driver.php +++ b/src/think/db/Driver.php @@ -209,7 +209,7 @@ public function connect($config = [], $linkNum = 0, $autoConnection = false) */ protected function parseDsn($config): string { - + return ''; } /**