mirror of https://gitee.com/karson/fastadmin.git
Backend 默认CURD Traits控制器 放置钩子
parent
2000a876dd
commit
06ec9b0352
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace app\admin\library\traits;
|
||||
use think\Hook;
|
||||
|
||||
trait Backend
|
||||
{
|
||||
|
|
@ -84,7 +85,12 @@ trait Backend
|
|||
$validate = is_bool($this->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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue