Browse Source

Update : 新增移除限制条件

朱金辉 3 weeks ago
parent
commit
5aa4347e15
3 changed files with 30 additions and 3 deletions
  1. 1 1
      src/Driver/Base.php
  2. 27 2
      src/Driver/Entity/Base.php
  3. 2 0
      src/Driver/Entity/Entity.php

+ 1 - 1
src/Driver/Base.php

@@ -196,7 +196,7 @@ class Base
     protected function formatInsertObject($arr) {
         if(is_array($arr)) {
             $values = array();
-            foreach ($arr as $v) {
+            foreach ($arr as $val) {
                 if(strtoupper(gettype($val)) == 'NULL') {
                     $values[] = 'null';
                     continue;

+ 27 - 2
src/Driver/Entity/Base.php

@@ -3,6 +3,7 @@ namespace Qii\Driver\Entity;
 
 
 
+use Qii\Driver\Model;
 use Qii\Driver\Response;
 
 class Base {
@@ -18,6 +19,11 @@ class Base {
      * @var array $config
      */
     public static $config;
+
+    /**
+     * @var Model $db
+     */
+    public static $db = null;
     /**
      * 主键
      * @var mix $privateKeys null
@@ -499,10 +505,26 @@ class Base {
      * @return \Qii\Driver\Base
      */
     public function db() {
+        if(self::$db !== null && self::$db instanceof Model) {
+            return self::$db;
+        }
         return _loadClass('\Qii\Driver\Model');
     }
 
     /**
+     * DB 必须是Model类型
+     *
+     * @param $db
+     * @return void
+     * @throws \Exception
+     */
+    final public function setDB($db) {
+        if(!($db instanceof \Qii\Driver\Model)) {
+            throw new \Exception('DB is illegal');
+        }
+        self::$db = $db;
+    }
+    /**
      * 返回 entity 信息
      * @return mixed
      */
@@ -1000,7 +1022,7 @@ class Base {
      * @return false|Response
      */
     public function remove() {
-        if(!$this->properties()) {
+        if(!$this->properties() && empty($this->getWhereHooker())) {
             return Response::Fail(static::class .'::'. __FUNCTION__,
             [
                 '_result' => ['code' => Response::FAIL_FOR_VALIDATE, 'msg' => '未指定删除目标', 'body' => []],
@@ -1067,7 +1089,6 @@ class Base {
                 'diffExclude' => $diffExclude
             ]
         );*/
-
         if(count($diffWhere) > 0 || count($diffOr) > 0) {
             $unique = $this->db()->limit(1)->where($diffWhere)->orTerms($diffOr)->exclude($exclude)->selectRow($this->prepareTable());
             if($unique) {
@@ -1686,6 +1707,10 @@ class Base {
      * @return array
      */
     protected function excludeCondition() {
+        $exclude = $this->exclude();
+        if(!$exclude) {
+            $exclude = $this->primaryKey();
+        }
         if(is_array($this->exclude())) {
             $exclude = $this->exclude();
         }else{

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

@@ -128,6 +128,7 @@ class Entity {
         $classAndProperty[] = "use Qii\Driver\Response;";
         $classAndProperty[] = <<<DOC
 /**
+ * @method $class where(\$where, \$args = null)
  * @method $class setWhereHooker(\$hooker, \$args = null)
  * @method $class with(\$with, \$args = null)
  * @method $class setWith(\$hooker, \$args = null)
@@ -135,6 +136,7 @@ class Entity {
  * @method $class setOrderHooker(\$hooker, \$args = null)
  * @method $class setHooker(\$key, \$hooker, \$args)
  * @method $class setCacheConfig(\$config)
+ * @method $class cache(\$cache, \$args = null)
  * @method $class setCacheHooker(\$hooker, \$args = null)
  * @method $class join(\$join, \$args = null)
  * @method $class setJoinHooker(\$hooker, \$args = null)