Prechádzať zdrojové kódy

删除动态,联动影响其他内容删除

lizhen_gitee 1 rok pred
rodič
commit
c30f8fe5cb

+ 69 - 1
application/admin/controller/Topicdongtai.php

@@ -3,7 +3,7 @@
 namespace app\admin\controller;
 
 use app\common\controller\Backend;
-
+use think\Db;
 /**
  * 用户动态
  *
@@ -73,4 +73,72 @@ class Topicdongtai extends Backend
         return $this->view->fetch();
     }
 
+    /**
+     * 删除
+     */
+    public function del($ids = "")
+    {
+        if (!$this->request->isPost()) {
+            $this->error(__("Invalid parameters"));
+        }
+        $ids = $ids ? $ids : $this->request->post("ids");
+        if ($ids) {
+            $pk = $this->model->getPk();
+            $adminIds = $this->getDataLimitAdminIds();
+            if (is_array($adminIds)) {
+                $this->model->where($this->dataLimitField, 'in', $adminIds);
+            }
+            $list = $this->model->where($pk, 'in', $ids)->select();
+
+            $count = 0;
+            Db::startTrans();
+            try {
+                foreach ($list as $k => $v) {
+                    $count ++;
+
+                    $this->delete_dongtai($v['id']);
+                }
+                Db::commit();
+            } catch (PDOException $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            } catch (Exception $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            }
+            if ($count) {
+                $this->success();
+            } else {
+                $this->error(__('No rows were deleted'));
+            }
+        }
+        $this->error(__('Parameter %s can not be empty', 'ids'));
+    }
+
+    //动态删除
+    public function delete_dongtai($id){
+
+        $where['id'] = $id;
+        $dongtai = Db::name('topic_dongtai')->field('id,topic_ids')->where($where)->find();
+        if (empty($dongtai)) {
+            return true;
+        }
+
+        $delRes = Db::name('topic_dongtai')->where('id',$id)->delete();
+
+        //话题少一个贴
+        if (!empty($dongtai['topic_ids'])) {
+            $res = Db::name('topic_hub')->where('id','IN',$dongtai['topic_ids'])->setDec('t_number');
+        }
+        //删除对应的评论,
+        Db::name('topic_dongtai_answer')->where('dt_id',$id)->delete();
+        //点赞,
+        Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();
+        //评论点赞
+        Db::name('topic_answer_good')->where('dt_id',$id)->delete();
+
+
+
+    }
+
 }

+ 3 - 0
application/admin/controller/Topicdongtaianswer.php

@@ -117,6 +117,9 @@ class Topicdongtaianswer extends Backend
     public function delete_answer($id){
 
         $info = Db::name('topic_dongtai_answer')->where('id',$id)->find();
+        if(!$info){
+            return true;
+        }
 
         if($info['level'] == 1){