From 497c2bb8f82b9375bba6114444741bc9b59850f9 Mon Sep 17 00:00:00 2001 From: tianheng2017 Date: Wed, 5 Aug 2020 19:56:04 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=81=B5=E6=B4=BB=E7=9A=84=E4=B8=BB?= =?UTF-8?q?=E9=94=AE=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 主键别写死了,数据库主键不一定都叫id呢 当前默认取得值是AdminController的pk 特殊情况下可被控制器的自定义pk覆盖 --- app/admin/traits/Curd.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/admin/traits/Curd.php b/app/admin/traits/Curd.php index 89bd1912..42a0aed1 100644 --- a/app/admin/traits/Curd.php +++ b/app/admin/traits/Curd.php @@ -77,9 +77,9 @@ public function add() /** * @NodeAnotation(title="编辑") */ - public function edit($id) + public function edit() { - $row = $this->model->find($id); + $row = $this->model->find($this->request->param($this->pk)); empty($row) && $this->error('数据不存在'); if ($this->request->isAjax()) { $post = $this->request->post(); @@ -99,9 +99,9 @@ public function edit($id) /** * @NodeAnotation(title="删除") */ - public function delete($id) + public function delete() { - $row = $this->model->whereIn('id', $id)->select(); + $row = $this->model->whereIn($this->pk, $this->request->param($this->pk))->select(); $row->isEmpty() && $this->error('数据不存在'); try { $save = $row->delete(); @@ -145,12 +145,12 @@ public function modify() { $post = $this->request->post(); $rule = [ - 'id|ID' => 'require', - 'field|字段' => 'require', - 'value|值' => 'require', + $this->pk.'|ID' => 'require', + 'field|字段' => 'require', + 'value|值' => 'require', ]; $this->validate($post, $rule); - $row = $this->model->find($post['id']); + $row = $this->model->find($post[$this->pk]); if (!$row) { $this->error('数据不存在'); } From a9d31a0cc4ef52829b4b54598635550ad556aa94 Mon Sep 17 00:00:00 2001 From: tianheng2017 Date: Sat, 15 Aug 2020 14:13:36 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BD=BF=E7=94=A8$this->model->getPk()?= =?UTF-8?q?=E4=BB=A3=E6=9B=BF=E5=86=99=E6=AD=BB=E7=9A=84id=E4=B8=BB?= =?UTF-8?q?=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/traits/Curd.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/admin/traits/Curd.php b/app/admin/traits/Curd.php index 42a0aed1..f01fa392 100644 --- a/app/admin/traits/Curd.php +++ b/app/admin/traits/Curd.php @@ -79,7 +79,7 @@ public function add() */ public function edit() { - $row = $this->model->find($this->request->param($this->pk)); + $row = $this->model->find($this->request->param($this->model->getPk())); empty($row) && $this->error('数据不存在'); if ($this->request->isAjax()) { $post = $this->request->post(); @@ -101,7 +101,7 @@ public function edit() */ public function delete() { - $row = $this->model->whereIn($this->pk, $this->request->param($this->pk))->select(); + $row = $this->model->whereIn($this->model->getPk(), $this->request->param($this->model->getPk()))->select(); $row->isEmpty() && $this->error('数据不存在'); try { $save = $row->delete(); @@ -131,7 +131,7 @@ public function export() $list = $this->model ->where($where) ->limit(100000) - ->order('id', 'desc') + ->order($this->model->getPk(), 'desc') ->select() ->toArray(); $fileName = time(); @@ -145,12 +145,12 @@ public function modify() { $post = $this->request->post(); $rule = [ - $this->pk.'|ID' => 'require', + $this->model->getPk().'|ID' => 'require', 'field|字段' => 'require', 'value|值' => 'require', ]; $this->validate($post, $rule); - $row = $this->model->find($post[$this->pk]); + $row = $this->model->find($post[$this->model->getPk()]); if (!$row) { $this->error('数据不存在'); } From 8996b688830be84e83a6755fffcc036794789812 Mon Sep 17 00:00:00 2001 From: tianheng2017 Date: Sat, 15 Aug 2020 14:21:00 +0800 Subject: [PATCH 3/3] Update Curd.php --- app/admin/traits/Curd.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/admin/traits/Curd.php b/app/admin/traits/Curd.php index f01fa392..8a39cc67 100644 --- a/app/admin/traits/Curd.php +++ b/app/admin/traits/Curd.php @@ -145,7 +145,7 @@ public function modify() { $post = $this->request->post(); $rule = [ - $this->model->getPk().'|ID' => 'require', + $this->model->getPk().'|主键' => 'require', 'field|字段' => 'require', 'value|值' => 'require', ];