From 06ec9b0352e03a8dd3e67906d7d30c0931c4b546 Mon Sep 17 00:00:00 2001 From: paul <748067867@qq.com> Date: Tue, 27 Nov 2018 11:24:25 +0800 Subject: [PATCH] =?UTF-8?q?Backend=20=E9=BB=98=E8=AE=A4CURD=20Traits?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=99=A8=20=E6=94=BE=E7=BD=AE=E9=92=A9?= =?UTF-8?q?=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/library/traits/Backend.php | 79 ++++++++++++++------ 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/application/admin/library/traits/Backend.php b/application/admin/library/traits/Backend.php index 89b73ac09..f5a558930 100755 --- a/application/admin/library/traits/Backend.php +++ b/application/admin/library/traits/Backend.php @@ -1,6 +1,7 @@ modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validate($validate); } - $result = $this->model->allowField(true)->save($params); + if (Hook::get('common_model_add')) { + $model = $this->model; + $result = Hook::listen("common_model_add", $model, $params); + } else { + $result = $this->model->allowField(true)->save($params); + } if ($result !== false) { $this->success(); } else { @@ -125,7 +131,11 @@ trait Backend $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validate($validate); } - $result = $row->allowField(true)->save($params); + if (Hook::get('common_model_edit')) { + $result = Hook::listen("common_model_edit", $row, $params); + } else { + $result = $row->allowField(true)->save($params); + } if ($result !== false) { $this->success(); } else { @@ -154,10 +164,15 @@ trait Backend if (is_array($adminIds)) { $count = $this->model->where($this->dataLimitField, 'in', $adminIds); } - $list = $this->model->where($pk, 'in', $ids)->select(); - $count = 0; - foreach ($list as $k => $v) { - $count += $v->delete(); + if (Hook::get('common_model_del')) { + $model = $this->model; + $count = Hook::listen("common_model_del", $model, [$pk,$ids]); + } else { + $list = $this->model->where($pk, 'in', $ids)->select(); + $count = 0; + foreach ($list as $k => $v) { + $count += $v->delete(); + } } if ($count) { $this->success(); @@ -178,13 +193,18 @@ trait Backend if (is_array($adminIds)) { $count = $this->model->where($this->dataLimitField, 'in', $adminIds); } - if ($ids) { - $this->model->where($pk, 'in', $ids); - } - $count = 0; - $list = $this->model->onlyTrashed()->select(); - foreach ($list as $k => $v) { - $count += $v->delete(true); + if (Hook::get('common_model_destroy')) { + $model = $this->model; + $count = Hook::listen("common_model_destroy", $model, [$pk,$ids]); + } else { + if ($ids) { + $this->model->where($pk, 'in', $ids); + } + $count = 0; + $list = $this->model->onlyTrashed()->select(); + foreach ($list as $k => $v) { + $count += $v->delete(true); + } } if ($count) { $this->success(); @@ -204,13 +224,18 @@ trait Backend if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } - if ($ids) { - $this->model->where($pk, 'in', $ids); - } - $count = 0; - $list = $this->model->onlyTrashed()->select(); - foreach ($list as $index => $item) { - $count += $item->restore(); + if (Hook::get('common_model_restore')) { + $model = $this->model; + $count = Hook::listen("common_model_restore", $model, [$pk,$ids]); + } else { + if ($ids) { + $this->model->where($pk, 'in', $ids); + } + $count = 0; + $list = $this->model->onlyTrashed()->select(); + foreach ($list as $index => $item) { + $count += $item->restore(); + } } if ($count) { $this->success(); @@ -224,6 +249,7 @@ trait Backend public function multi($ids = "") { $ids = $ids ? $ids : $this->request->param("ids"); + $pk = $this->model->getPk(); if ($ids) { if ($this->request->has('params')) { parse_str($this->request->post("params"), $values); @@ -233,10 +259,15 @@ trait Backend if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } - $count = 0; - $list = $this->model->where($this->model->getPk(), 'in', $ids)->select(); - foreach ($list as $index => $item) { - $count += $item->allowField(true)->isUpdate(true)->save($values); + if (Hook::get('common_model_multi')) { + $model = $this->model; + $count = Hook::listen("common_model_multi", $model, [$pk,$ids,$values]); + } else { + $count = 0; + $list = $this->model->where($pk, 'in', $ids)->select(); + foreach ($list as $index => $item) { + $count += $item->allowField(true)->isUpdate(true)->save($values); + } } if ($count) { $this->success(); -- Gitee