Parcourir la source

分类创建时唯一性判断

Home il y a 4 ans
Parent
commit
5f39b7ac98
1 fichiers modifiés avec 14 ajouts et 2 suppressions
  1. 14 2
      app/common/business/Category.php

+ 14 - 2
app/common/business/Category.php

@@ -17,12 +17,24 @@ class Category
     /**
      * 新增一条分类
      * @param array $data
-     * @return int
+     * @return mixed
+     * @throws \think\Exception
      */
     public function add(array $data)
     {
         $data['status'] = config('status.mysql.table_normal');
-        $this->categoryObj->save($data);
+
+        $name = $data['name'];
+        $count = $this->categoryObj->where('name',$name)->count();
+        if ($count > 0) {
+            throw new \think\Exception('分类名已经存在');
+        }
+
+        try {
+            $this->categoryObj->save($data);
+        } catch (\Exception $e) {
+            throw new \think\Exception('服务内部异常');
+        }
         return $this->categoryObj->getLastInsID();
     }
 }