Skip to content

Commit

Permalink
Merge pull request #49 from teamones-open/add_hint
Browse files Browse the repository at this point in the history
禁用DB after event 回调,防止死循环事件产生
  • Loading branch information
weijer committed Nov 29, 2023
2 parents eef76d1 + 63ee081 commit 15c2af8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/think/Model.php
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/think/db/Driver.php
Expand Up @@ -209,7 +209,7 @@ public function connect($config = [], $linkNum = 0, $autoConnection = false)
*/
protected function parseDsn($config): string
{

return '';
}

/**
Expand Down Expand Up @@ -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';
Expand Down
25 changes: 25 additions & 0 deletions src/think/model/RelationModel.php
Expand Up @@ -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']);
Expand All @@ -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']);
Expand All @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit 15c2af8

Please sign in to comment.