Browse Source

add remove method

朱金辉 1 year ago
parent
commit
a27b03bd2f
3 changed files with 62 additions and 2 deletions
  1. 35 1
      src/Driver/Entity/Base.php
  2. 27 0
      src/Driver/Entity/Entity.php
  3. 0 1
      src/Library/Third/hightman/Client.php

+ 35 - 1
src/Driver/Entity/Base.php

@@ -260,6 +260,35 @@ class Base {
             ['_result' => ['code' => Response::DO_SUCCESS, 'msg' => '添加成功', 'body' => $res]]
         );
     }
+
+    /**
+     * 删除指定数据
+     *
+     * @return false|Response
+     */
+    public function remove() {
+        if(!$this->properties()) {
+            return false;
+        }
+        $properties = [];
+        foreach ($this->properties() as $key => $val) {
+            $properties[] = $this->entity()->convertToProperty($key);
+        }
+
+        $valid = $this->valid($properties);
+        if($valid->isError()) {
+            return $valid;
+        }
+        $affectedRows = $this->db()->where($this->properties())->delete($this->getTable());
+        if($this->db()->isError()) {
+            return Response::FailSave(static::class .'::'. __FUNCTION__,
+                ['_result' => ['code' => Response::FAIL_FOR_SAVE, 'msg' => $this->db()->getMessage(), 'body' => []]
+                ]);
+        }
+        return Response::Success(static::class .'::'. __FUNCTION__,
+            ['_result' => ['code' => Response::DO_SUCCESS, 'msg' => '删除成功,总共删除了'. $affectedRows . '条记录', 'body' => $affectedRows]]
+        );
+    }
     /**
      * 更新
      *
@@ -271,7 +300,6 @@ class Base {
         if($valid->isError()) {
             return $valid;
         }
-        return $this->updateFields();
         //检查是否有重复的
         list($uniqueWhere, $uniqueOr, $exclude, $primaryKey) = $this->condition();
         // 检查 unique 是否已经存在相关数据
@@ -326,6 +354,12 @@ class Base {
         );
     }
 
+    /**
+     * 更新字段
+     *
+     * @return mixed|\Qii\Driver\Qii\Driver\Response|Response
+     * @throws \Exception
+     */
     public function updateFields() {
         $properties = $this->properties();
         $fields = $this->entity()->convertToProperties(array_keys($properties));

+ 27 - 0
src/Driver/Entity/Entity.php

@@ -230,8 +230,35 @@ DOC;
         $next[] = "\t\treturn ". '$rules;';
         $next[] = "\t". '}';
         $next[] = <<<DOC
+    /**
+     * unique (unique 如果是 array 则表示 联合唯一,如果是string则是任意唯一,多个单独唯一使用字符串以逗号隔开, 主键除外)
+     *
+     * @return mixed
+     * @throws \Exception
+     */
+    public function uniqueKey(){
+        throw new \Exception('请设置唯一值');
+    }
+
+    /**
+     * 主键
+     *
+     * @return mixed
+     * @throws \Exception
+     */
+    public function primaryKey(){
+        throw new \Exception('请设置主键');
+    }
 
     /**
+     * 保存数据的时候,验证唯一需要排除的值,此处仅支持,单个或联合排除,不支持单个排除
+     *
+     * @return array
+     */
+    public function exclude(){
+        return array();
+    }
+    /**
      * 添加时验证的字段,自行添加
      */
     public function validFieldsForAdd(){

+ 0 - 1
src/Library/Third/hightman/Client.php

@@ -130,7 +130,6 @@ class Client
      */
     public function clearHeader()
     {
-        parent::clearHeader();
         $this->applyDefaultHeader();
     }