1
0

2 Commits 9ab2bba2f7 ... 5f39b7ac98

Autor SHA1 Mensagem Data
  Home 5f39b7ac98 分类创建时唯一性判断 há 4 anos atrás
  Home fc75a0371a 8-4分类表新增索引 há 4 anos atrás
2 ficheiros alterados com 17 adições e 3 exclusões
  1. 14 2
      app/common/business/Category.php
  2. 3 1
      mysql.sql

+ 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();
     }
 }

+ 3 - 1
mysql.sql

@@ -46,5 +46,7 @@ CREATE TABLE `mall_category` (
   `operate_user` varchar(100) NOT NULL COMMENT '操作人',
   `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '',
   `listorder` int(10) unsigned unsigned NOT NULL DEFAULT '0' COMMENT '排序',
-  PRIMARY KEY (`id`) USING BTREE
+  PRIMARY KEY (`id`) USING BTREE,
+  KEY `pid` (`pid`),
+  KEY `name` (`name`)
 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;